/// <summary> /// Creates process start information. /// </summary> /// <param name="commandLineArgs">The command line arguments.</param> /// <returns>An instance of <see cref="ProcessStartInfo"/>.</returns> public static ProcessStartInfo CreateProcessStartInfo(string[] commandLineArgs) { if (commandLineArgs.Length < 2) { return(null); } string executable = commandLineArgs[1]; CommandEscaper commandEscaper = new CommandEscaper(); IEnumerable <string> escapedArguments = commandLineArgs.Skip(2).Select(a => commandEscaper.Escape(a)); string arguments = string.Join(" ", escapedArguments); ProcessStartInfo processStartInfo = new ProcessStartInfo(executable, arguments) { UseShellExecute = false }; return(processStartInfo); }
/// <summary> /// Executes the progam. /// </summary> /// <param name="args">Command line arguments.</param> public static void Main(string[] args) { Debugging.PrintDebugBuild(); /* In this array, the first item (at 0) will be the path to the current * executable. The second item (at 1) will be the executable to be * invoked. Further arguments are input to the other process. */ string[] commandLineArgs = Environment.GetCommandLineArgs(); if (RuntimeChecks.CheckHelp(commandLineArgs)) { return; } if (RuntimeChecks.CheckAliases(commandLineArgs)) { return; } ProcessStartInfo processStartInfo; if (commandLineArgs.Length < 2) { processStartInfo = new ProcessStartInfo(Settings.GetDefaultExecutable()) { UseShellExecute = true, Verb = "runas" }; } else if (commandLineArgs[1] == "-c" || commandLineArgs[1] == "-C") { bool leaveOpen = commandLineArgs[1] == "-C"; CommandEscaper commandEscaper = new CommandEscaper(); IEnumerable <string> escapedArguments = commandLineArgs.Skip(2).Select(a => commandEscaper.Escape(a)); string passedArguments = string.Join(" ", escapedArguments); processStartInfo = new ProcessStartInfo("cmd", $"/{(leaveOpen ? "K" : "C")} {passedArguments}") { UseShellExecute = true, Verb = "runas" }; } else { string executable = commandLineArgs[1]; CommandEscaper commandEscaper = new CommandEscaper(); IEnumerable <string> escapedArguments = commandLineArgs.Skip(2).Select(a => commandEscaper.Escape(a)); string arguments = string.Join(" ", escapedArguments); processStartInfo = new ProcessStartInfo(executable, arguments) { UseShellExecute = true, Verb = "runas" }; } ProcessUtilities.RunSafe(processStartInfo, false); }