< Summary

Information
Class: MyNet.Primitives.Helpers.ProcessHelper
Assembly: MyNet.Primitives
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Primitives/Helpers/ProcessHelper.cs
Tag: 323_28699572109
Line coverage
69%
Covered lines: 16
Uncovered lines: 7
Coverable lines: 23
Total lines: 104
Line coverage: 69.5%
Branch coverage
100%
Covered branches: 2
Total branches: 2
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Start(...)100%1133.33%
TryStart(...)100%1180%
Open(...)100%22100%
OpenFile(...)100%1133.33%
OpenInExcel(...)100%1133.33%
OpenFolder(...)100%11100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Primitives/Helpers/ProcessHelper.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="ProcessHelper.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Collections.Generic;
 9using System.Diagnostics;
 10
 11namespace MyNet.Primitives.Helpers;
 12
 13/// <summary>
 14/// Provides helper methods for starting processes and opening files or folders.
 15/// </summary>
 16public static class ProcessHelper
 17{
 18    /// <summary>
 19    /// Starts a process using the specified URI. The URI can be a file path, folder path, or URL. The method uses the d
 20    /// </summary>
 21    /// <param name="uri">The URI to open.</param>
 22    /// <exception cref="ArgumentException">Thrown when the URI is null or whitespace.</exception>
 23    public static void Start(string uri)
 24    {
 1025        ArgumentException.ThrowIfNullOrWhiteSpace(uri);
 26
 027        _ = Process.Start(new ProcessStartInfo { FileName = uri, UseShellExecute = true });
 028    }
 29
 30    /// <summary>
 31    /// Attempts to start a process using the specified URI. Returns <c>true</c> if the process was started successfully
 32    /// </summary>
 33    /// <param name="uri">The URI to open.</param>
 34    /// <returns><c>true</c> if the process was started successfully; otherwise, <c>false</c>.</returns>
 35    public static bool TryStart(string uri)
 36    {
 37        try
 38        {
 539            Start(uri);
 040            return true;
 41        }
 542        catch
 43        {
 544            return false;
 45        }
 546    }
 47
 48    /// <summary>
 49    /// Starts a process using the specified executable and arguments. The method uses the default application associate
 50    /// </summary>
 51    /// <param name="executable">The executable to run.</param>
 52    /// <param name="arguments">The arguments to pass to the executable.</param>
 53    /// <exception cref="ArgumentException">Thrown when <paramref name="executable"/> is null or whitespace.</exception>
 54    public static void Open(string executable, params IEnumerable<string> arguments)
 55    {
 856        ArgumentException.ThrowIfNullOrWhiteSpace(executable);
 57
 358        var startInfo = new ProcessStartInfo { FileName = executable, UseShellExecute = true };
 59
 1260        foreach (var argument in arguments)
 61        {
 362            startInfo.ArgumentList.Add(argument);
 63        }
 64
 365        _ = Process.Start(startInfo);
 366    }
 67
 68    /// <summary>
 69    /// Opens a file using the default application associated with its file type. The method validates that the provided
 70    /// </summary>
 71    /// <param name="filePath">The path of the file to open.</param>
 72    /// <exception cref="ArgumentException">Thrown when <paramref name="filePath"/> is null or whitespace.</exception>
 73    public static void OpenFile(string filePath)
 74    {
 575        ArgumentException.ThrowIfNullOrWhiteSpace(filePath);
 76
 077        Start(filePath);
 078    }
 79
 80    /// <summary>
 81    /// Opens a file in Excel using the default application associated with its file type. This method is essentially an
 82    /// </summary>
 83    /// <param name="filePath">The path of the file to open in Excel.</param>
 84    /// <exception cref="ArgumentException">Thrown when <paramref name="filePath"/> is null or whitespace.</exception>
 85    public static void OpenInExcel(string filePath)
 86    {
 587        ArgumentException.ThrowIfNullOrWhiteSpace(filePath);
 88
 089        OpenFile(filePath);
 090    }
 91
 92    /// <summary>
 93    /// Opens a folder using the default file explorer. The method validates that the provided folder path is not null o
 94    /// </summary>
 95    /// <param name="folderPath">The path of the folder to open.</param>
 96    /// <exception cref="ArgumentException">Thrown when <paramref name="folderPath"/> is null or whitespace.</exception>
 97    public static void OpenFolder(string folderPath)
 98    {
 899        ArgumentException.ThrowIfNullOrWhiteSpace(folderPath);
 100
 3101        Open("explorer.exe", folderPath);
 3102    }
 103}
 104