private ProcessStartInfo GetProcessStartInfo(string programArguments, RunAsType runAs = RunAsType.None) { return(new ProcessStartInfo { FileName = LnkResolvedPath ?? FullPath, WorkingDirectory = ParentDirectory, UseShellExecute = true, Arguments = programArguments, Verb = runAs == RunAsType.Administrator ? "runAs" : runAs == RunAsType.OtherUser ? "runAsUser" : string.Empty, }); }
private ProcessStartInfo PrepareProcessStartInfo(string command, RunAsType runAs = RunAsType.None) { string trimmedCommand = command.Trim(); command = Environment.ExpandEnvironmentVariables(trimmedCommand); var workingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); // Set runAsArg string runAsVerbArg = string.Empty; if (runAs == RunAsType.OtherUser) { runAsVerbArg = "runAsUser"; } else if (runAs == RunAsType.Administrator || _settings.RunAsAdministrator) { runAsVerbArg = "runAs"; } ProcessStartInfo info; if (_settings.Shell == ExecutionShell.Cmd) { var arguments = _settings.LeaveShellOpen ? $"/k \"{command}\"" : $"/c \"{command}\" & pause"; info = ShellCommand.SetProcessStartInfo("cmd.exe", workingDirectory, arguments, runAsVerbArg); } else if (_settings.Shell == ExecutionShell.Powershell) { string arguments; if (_settings.LeaveShellOpen) { arguments = $"-NoExit \"{command}\""; } else { arguments = $"\"{command} ; Read-Host -Prompt \\\"Press Enter to continue\\\"\""; } info = ShellCommand.SetProcessStartInfo("powershell.exe", workingDirectory, arguments, runAsVerbArg); } else if (_settings.Shell == ExecutionShell.RunCommand) { // Open explorer if the path is a file or directory if (Directory.Exists(command) || File.Exists(command)) { info = ShellCommand.SetProcessStartInfo("explorer.exe", arguments: command, verb: runAsVerbArg); } else { var parts = command.Split(new[] { ' ' }, 2); if (parts.Length == 2) { var filename = parts[0]; if (ExistInPath(filename)) { var arguments = parts[1]; info = ShellCommand.SetProcessStartInfo(filename, workingDirectory, arguments, runAsVerbArg); } else { info = ShellCommand.SetProcessStartInfo(command, verb: runAsVerbArg); } } else { info = ShellCommand.SetProcessStartInfo(command, verb: runAsVerbArg); } } } else { throw new NotImplementedException(); } info.UseShellExecute = true; _settings.AddCmdHistory(trimmedCommand); return(info); }