diff --git a/Plugins/Wox.Plugin.Shell/Languages/en.xaml b/Plugins/Wox.Plugin.Shell/Languages/en.xaml index 7a953986cc..6f56e83f58 100644 --- a/Plugins/Wox.Plugin.Shell/Languages/en.xaml +++ b/Plugins/Wox.Plugin.Shell/Languages/en.xaml @@ -4,6 +4,7 @@ Replace Win+R Do not close Command Prompt after command execution + Always run as administrator Shell Allows to execute system commands from Wox. Commands should start with > this command has been executed {0} times diff --git a/Plugins/Wox.Plugin.Shell/Main.cs b/Plugins/Wox.Plugin.Shell/Main.cs index 1e60ef8c9c..323cfb5cd3 100644 --- a/Plugins/Wox.Plugin.Shell/Main.cs +++ b/Plugins/Wox.Plugin.Shell/Main.cs @@ -9,6 +9,7 @@ using WindowsInput.Native; using Wox.Infrastructure.Hotkey; using Wox.Infrastructure.Logger; using Wox.Infrastructure.Storage; +using Wox.Plugin.SharedCommands; using Application = System.Windows.Application; using Control = System.Windows.Controls.Control; using Keys = System.Windows.Forms.Keys; @@ -164,16 +165,15 @@ namespace Wox.Plugin.Shell { command = command.Trim(); command = Environment.ExpandEnvironmentVariables(command); + var workingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); + var runAsAdministratorArg = !runAsAdministrator && !_settings.RunAsAdministrator ? "" : "runas"; ProcessStartInfo info; if (_settings.Shell == Shell.Cmd) { var arguments = _settings.LeaveShellOpen ? $"/k \"{command}\"" : $"/c \"{command}\" & pause"; - info = new ProcessStartInfo - { - FileName = "cmd.exe", - Arguments = arguments, - }; + + info = ShellCommand.SetProcessStartInfo("cmd.exe", workingDirectory, arguments, runAsAdministratorArg); } else if (_settings.Shell == Shell.Powershell) { @@ -186,11 +186,8 @@ namespace Wox.Plugin.Shell { arguments = $"\"{command} ; Read-Host -Prompt \\\"Press Enter to continue\\\"\""; } - info = new ProcessStartInfo - { - FileName = "powershell.exe", - Arguments = arguments - }; + + info = ShellCommand.SetProcessStartInfo("powershell.exe", workingDirectory, arguments, runAsAdministratorArg); } else if (_settings.Shell == Shell.RunCommand) { @@ -200,21 +197,17 @@ namespace Wox.Plugin.Shell var filename = parts[0]; if (ExistInPath(filename)) { - var arguemtns = parts[1]; - info = new ProcessStartInfo - { - FileName = filename, - Arguments = arguemtns - }; + var arguments = parts[1]; + info = ShellCommand.SetProcessStartInfo(filename, workingDirectory, arguments, runAsAdministratorArg); } else { - info = new ProcessStartInfo(command); + info = ShellCommand.SetProcessStartInfo(command, verb: runAsAdministratorArg); } } else { - info = new ProcessStartInfo(command); + info = ShellCommand.SetProcessStartInfo(command, verb: runAsAdministratorArg); } } else @@ -222,10 +215,7 @@ namespace Wox.Plugin.Shell return; } - info.UseShellExecute = true; - info.WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); - info.Verb = runAsAdministrator ? "runas" : ""; try { diff --git a/Plugins/Wox.Plugin.Shell/Settings.cs b/Plugins/Wox.Plugin.Shell/Settings.cs index 62eb31f4e3..af149e8290 100644 --- a/Plugins/Wox.Plugin.Shell/Settings.cs +++ b/Plugins/Wox.Plugin.Shell/Settings.cs @@ -7,6 +7,8 @@ namespace Wox.Plugin.Shell public Shell Shell { get; set; } = Shell.Cmd; public bool ReplaceWinR { get; set; } = true; public bool LeaveShellOpen { get; set; } + public bool RunAsAdministrator { get; set; } = true; + public Dictionary Count = new Dictionary(); public void AddCmdHistory(string cmdName) diff --git a/Plugins/Wox.Plugin.Shell/ShellSetting.xaml b/Plugins/Wox.Plugin.Shell/ShellSetting.xaml index f631f6e228..dc6de53bac 100644 --- a/Plugins/Wox.Plugin.Shell/ShellSetting.xaml +++ b/Plugins/Wox.Plugin.Shell/ShellSetting.xaml @@ -12,10 +12,12 @@ + - + + CMD PowerShell RunCommand diff --git a/Plugins/Wox.Plugin.Shell/ShellSetting.xaml.cs b/Plugins/Wox.Plugin.Shell/ShellSetting.xaml.cs index 639c5c3c98..ffa3b58568 100644 --- a/Plugins/Wox.Plugin.Shell/ShellSetting.xaml.cs +++ b/Plugins/Wox.Plugin.Shell/ShellSetting.xaml.cs @@ -17,6 +17,7 @@ namespace Wox.Plugin.Shell { ReplaceWinR.IsChecked = _settings.ReplaceWinR; LeaveShellOpen.IsChecked = _settings.LeaveShellOpen; + AlwaysRunAsAdministrator.IsChecked = _settings.RunAsAdministrator; LeaveShellOpen.IsEnabled = _settings.Shell != Shell.RunCommand; LeaveShellOpen.Checked += (o, e) => @@ -29,6 +30,16 @@ namespace Wox.Plugin.Shell _settings.LeaveShellOpen = false; }; + AlwaysRunAsAdministrator.Checked += (o, e) => + { + _settings.RunAsAdministrator = true; + }; + + AlwaysRunAsAdministrator.Unchecked += (o, e) => + { + _settings.RunAsAdministrator = false; + }; + ReplaceWinR.Checked += (o, e) => { _settings.ReplaceWinR = true; diff --git a/Wox.Plugin/SharedCommands/ShellCommand.cs b/Wox.Plugin/SharedCommands/ShellCommand.cs new file mode 100644 index 0000000000..d7071e7353 --- /dev/null +++ b/Wox.Plugin/SharedCommands/ShellCommand.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Wox.Plugin.SharedCommands +{ + public static class ShellCommand + { + public static ProcessStartInfo SetProcessStartInfo(this string fileName, string workingDirectory="", string arguments = "", string verb = "") + { + var info = new ProcessStartInfo + { + FileName = fileName, + WorkingDirectory = workingDirectory, + Arguments = arguments, + Verb = verb + }; + + return info; + } + } +} diff --git a/Wox.Plugin/Wox.Plugin.csproj b/Wox.Plugin/Wox.Plugin.csproj index 843cc7d8bf..3a486fb42b 100644 --- a/Wox.Plugin/Wox.Plugin.csproj +++ b/Wox.Plugin/Wox.Plugin.csproj @@ -78,6 +78,7 @@ +