/// <summary> /// Starts an process using the passed parameters. /// </summary> /// <param name="filename">The filename to execute.</param> /// <param name="arguments">The arguments to pass to the process. This parameter is optional.</param> /// <param name="workingDirectory">The workingdirectory of the process. This parameter is optional.</param> /// <param name="operation">The operation to be performed. This parameter is optional. By default, <see cref="ProcessVerb.Open"/> will be used.</param> /// <param name="visibility">A <see cref="ProcessVisibility"/>-value to determine how the new process will be shown. This parameter is optional. By default, <see cref="ProcessVisibility.Normal"/> will be used.</param> /// <param name="suppressErrors">A value determining if exception will be supressed or not.</param> /// <returns><code>true</code> if the operation succeeds, otherwise <code>false</code>.</returns> public static bool ShellExecute(string filename, string arguments = null, string workingDirectory = null, ProcessVerb operation = ProcessVerb.Open, ProcessVisibility visibility = ProcessVisibility.Normal, bool suppressErrors = true) { if (!AutomationFactory.IsAvailable) { throw new Exception("Automation unavailable due to insufficient rights."); } if (filename == null) { throw new ArgumentNullException("commandline"); } if (String.IsNullOrWhiteSpace(filename)) { throw new ArgumentException("Invalid commandline.", "commandline"); } var args = arguments ?? ""; var dir = workingDirectory ?? ""; var verb = operation.ToString().ToLowerInvariant(); int mode = (int)visibility; dynamic shell = null; try { using (shell = AutomationFactory.CreateObject("Shell.Application")) { shell.ShellExecute(filename, args, dir, verb, mode); } } catch (Exception ex) { #if DEBUG Debug.WriteLine(ex.ToString()); #endif if (suppressErrors) { return(false); } throw ex; } finally { shell = null; GC.Collect(); GC.WaitForPendingFinalizers(); } return(true); }
/// <summary> /// Starts an process using the passed parameters. /// </summary> /// <param name="filename">The filename to execute.</param> /// <param name="arguments">The arguments to pass to the process. This parameter is optional.</param> /// <param name="workingDirectory">The workingdirectory of the process. This parameter is optional.</param> /// <param name="operation">The operation to be performed. This parameter is optional. By default, <see cref="ProcessVerb.Open"/> will be used.</param> /// <param name="visibility">A <see cref="ProcessVisibility"/>-value to determine how the new process will be shown. This parameter is optional. By default, <see cref="ProcessVisibility.Normal"/> will be used.</param> /// <param name="suppressErrors">A value determining if exception will be supressed or not.</param> /// <returns><code>true</code> if the operation succeeds, otherwise <code>false</code>.</returns> public static bool ShellExecute(string filename, string arguments = null, string workingDirectory = null, ProcessVerb operation = ProcessVerb.Open, ProcessVisibility visibility = ProcessVisibility.Normal, bool suppressErrors = true) { if (!AutomationFactory.IsAvailable) throw new Exception("Automation unavailable due to insufficient rights."); if (filename == null) throw new ArgumentNullException("commandline"); if (String.IsNullOrWhiteSpace(filename)) throw new ArgumentException("Invalid commandline.", "commandline"); var args = arguments ?? ""; var dir = workingDirectory ?? ""; var verb = operation.ToString().ToLowerInvariant(); int mode = (int)visibility; dynamic shell = null; try { using (shell = AutomationFactory.CreateObject("Shell.Application")) { shell.ShellExecute(filename, args, dir, verb, mode); } } catch (Exception ex) { #if DEBUG Debug.WriteLine(ex.ToString()); #endif if (suppressErrors) return false; throw ex; } finally { shell = null; GC.Collect(); GC.WaitForPendingFinalizers(); } return true; }