| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ProcessHelper.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.Diagnostics; |
| | | 10 | | |
| | | 11 | | namespace MyNet.Primitives.Helpers; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Provides helper methods for starting processes and opening files or folders. |
| | | 15 | | /// </summary> |
| | | 16 | | public 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 | | { |
| | 10 | 25 | | ArgumentException.ThrowIfNullOrWhiteSpace(uri); |
| | | 26 | | |
| | 0 | 27 | | _ = Process.Start(new ProcessStartInfo { FileName = uri, UseShellExecute = true }); |
| | 0 | 28 | | } |
| | | 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 | | { |
| | 5 | 39 | | Start(uri); |
| | 0 | 40 | | return true; |
| | | 41 | | } |
| | 5 | 42 | | catch |
| | | 43 | | { |
| | 5 | 44 | | return false; |
| | | 45 | | } |
| | 5 | 46 | | } |
| | | 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 | | { |
| | 8 | 56 | | ArgumentException.ThrowIfNullOrWhiteSpace(executable); |
| | | 57 | | |
| | 3 | 58 | | var startInfo = new ProcessStartInfo { FileName = executable, UseShellExecute = true }; |
| | | 59 | | |
| | 12 | 60 | | foreach (var argument in arguments) |
| | | 61 | | { |
| | 3 | 62 | | startInfo.ArgumentList.Add(argument); |
| | | 63 | | } |
| | | 64 | | |
| | 3 | 65 | | _ = Process.Start(startInfo); |
| | 3 | 66 | | } |
| | | 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 | | { |
| | 5 | 75 | | ArgumentException.ThrowIfNullOrWhiteSpace(filePath); |
| | | 76 | | |
| | 0 | 77 | | Start(filePath); |
| | 0 | 78 | | } |
| | | 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 | | { |
| | 5 | 87 | | ArgumentException.ThrowIfNullOrWhiteSpace(filePath); |
| | | 88 | | |
| | 0 | 89 | | OpenFile(filePath); |
| | 0 | 90 | | } |
| | | 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 | | { |
| | 8 | 99 | | ArgumentException.ThrowIfNullOrWhiteSpace(folderPath); |
| | | 100 | | |
| | 3 | 101 | | Open("explorer.exe", folderPath); |
| | 3 | 102 | | } |
| | | 103 | | } |
| | | 104 | | |