From 92bde25a49fdbc25febe7c1cb5a1cc9a4a8fb265 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Fri, 6 Dec 2019 07:49:20 +1100 Subject: [PATCH 1/5] Add option to run as administrator for Shell plugin --- Plugins/Wox.Plugin.Program/Programs/Win32.cs | 10 ++------ Wox.Plugin/SharedCommands/ShellCommand.cs | 24 ++++++++++++++++++++ Wox.Plugin/Wox.Plugin.csproj | 1 + 3 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 Wox.Plugin/SharedCommands/ShellCommand.cs diff --git a/Plugins/Wox.Plugin.Program/Programs/Win32.cs b/Plugins/Wox.Plugin.Program/Programs/Win32.cs index 36e08a3c46..f0697abef3 100644 --- a/Plugins/Wox.Plugin.Program/Programs/Win32.cs +++ b/Plugins/Wox.Plugin.Program/Programs/Win32.cs @@ -10,6 +10,7 @@ using Microsoft.Win32; using Shell; using Wox.Infrastructure; using Wox.Plugin.Program.Logger; +using Wox.Plugin.SharedCommands; namespace Wox.Plugin.Program.Programs { @@ -96,14 +97,7 @@ namespace Wox.Plugin.Program.Programs Title = api.GetTranslation("wox_plugin_program_run_as_administrator"), Action = _ => { - var info = new ProcessStartInfo - { - FileName = FullPath, - WorkingDirectory = ParentDirectory, - Verb = "runas" - }; - var hide = Main.StartProcess(info); - return hide; + return Main.StartProcess(ShellCommand.SetCMDRunAsAdministrator(FullPath, ParentDirectory)); }, IcoPath = "Images/cmd.png" }, diff --git a/Wox.Plugin/SharedCommands/ShellCommand.cs b/Wox.Plugin/SharedCommands/ShellCommand.cs new file mode 100644 index 0000000000..ac3f4e7cba --- /dev/null +++ b/Wox.Plugin/SharedCommands/ShellCommand.cs @@ -0,0 +1,24 @@ +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 SetCMDRunAsAdministrator(this string fullPath, string parentDirectory) + { + var info = new ProcessStartInfo + { + FileName = fullPath, + WorkingDirectory = parentDirectory, + Verb = "runas" + }; + + 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 @@ + From 201c26f7c853c0e758782eff2af18832f9504f32 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Tue, 10 Dec 2019 08:23:34 +1100 Subject: [PATCH 2/5] Add run as administrator to Shell plugin settings --- Plugins/Wox.Plugin.Program/Programs/Win32.cs | 2 +- Plugins/Wox.Plugin.Shell/Main.cs | 32 +++++++------------- Plugins/Wox.Plugin.Shell/Settings.cs | 2 ++ Wox.Plugin/SharedCommands/ShellCommand.cs | 9 +++--- 4 files changed, 19 insertions(+), 26 deletions(-) diff --git a/Plugins/Wox.Plugin.Program/Programs/Win32.cs b/Plugins/Wox.Plugin.Program/Programs/Win32.cs index f0697abef3..12b54b524c 100644 --- a/Plugins/Wox.Plugin.Program/Programs/Win32.cs +++ b/Plugins/Wox.Plugin.Program/Programs/Win32.cs @@ -97,7 +97,7 @@ namespace Wox.Plugin.Program.Programs Title = api.GetTranslation("wox_plugin_program_run_as_administrator"), Action = _ => { - return Main.StartProcess(ShellCommand.SetCMDRunAsAdministrator(FullPath, ParentDirectory)); + return Main.StartProcess(ShellCommand.SetProcessStartInfo(FullPath, ParentDirectory)); }, IcoPath = "Images/cmd.png" }, 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..121875308d 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; } + internal bool RunAsAdministrator { get; set; } = true; + public Dictionary Count = new Dictionary(); public void AddCmdHistory(string cmdName) diff --git a/Wox.Plugin/SharedCommands/ShellCommand.cs b/Wox.Plugin/SharedCommands/ShellCommand.cs index ac3f4e7cba..d7071e7353 100644 --- a/Wox.Plugin/SharedCommands/ShellCommand.cs +++ b/Wox.Plugin/SharedCommands/ShellCommand.cs @@ -9,13 +9,14 @@ namespace Wox.Plugin.SharedCommands { public static class ShellCommand { - public static ProcessStartInfo SetCMDRunAsAdministrator(this string fullPath, string parentDirectory) + public static ProcessStartInfo SetProcessStartInfo(this string fileName, string workingDirectory="", string arguments = "", string verb = "") { var info = new ProcessStartInfo { - FileName = fullPath, - WorkingDirectory = parentDirectory, - Verb = "runas" + FileName = fileName, + WorkingDirectory = workingDirectory, + Arguments = arguments, + Verb = verb }; return info; From b123d95b71520540c73e92d5dc776a7630f323ab Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Tue, 10 Dec 2019 20:03:43 +1100 Subject: [PATCH 3/5] Add run as admin toggle option to plugin settings --- Plugins/Wox.Plugin.Shell/Languages/en.xaml | 1 + Plugins/Wox.Plugin.Shell/ShellSetting.xaml | 4 +++- Plugins/Wox.Plugin.Shell/ShellSetting.xaml.cs | 11 +++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) 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/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; From c22ab4afdf2dfdb8a88351b273aeed3058dc3d28 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Tue, 10 Dec 2019 20:11:12 +1100 Subject: [PATCH 4/5] Revert changes to other plugin. Will refactor as a separate PR --- Plugins/Wox.Plugin.Program/Programs/Win32.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Plugins/Wox.Plugin.Program/Programs/Win32.cs b/Plugins/Wox.Plugin.Program/Programs/Win32.cs index 12b54b524c..36e08a3c46 100644 --- a/Plugins/Wox.Plugin.Program/Programs/Win32.cs +++ b/Plugins/Wox.Plugin.Program/Programs/Win32.cs @@ -10,7 +10,6 @@ using Microsoft.Win32; using Shell; using Wox.Infrastructure; using Wox.Plugin.Program.Logger; -using Wox.Plugin.SharedCommands; namespace Wox.Plugin.Program.Programs { @@ -97,7 +96,14 @@ namespace Wox.Plugin.Program.Programs Title = api.GetTranslation("wox_plugin_program_run_as_administrator"), Action = _ => { - return Main.StartProcess(ShellCommand.SetProcessStartInfo(FullPath, ParentDirectory)); + var info = new ProcessStartInfo + { + FileName = FullPath, + WorkingDirectory = ParentDirectory, + Verb = "runas" + }; + var hide = Main.StartProcess(info); + return hide; }, IcoPath = "Images/cmd.png" }, From e60e574902a582c8dbbc50fbc502c604a5faa8ba Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Tue, 10 Dec 2019 20:24:18 +1100 Subject: [PATCH 5/5] update --- Plugins/Wox.Plugin.Shell/Settings.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Plugins/Wox.Plugin.Shell/Settings.cs b/Plugins/Wox.Plugin.Shell/Settings.cs index 121875308d..af149e8290 100644 --- a/Plugins/Wox.Plugin.Shell/Settings.cs +++ b/Plugins/Wox.Plugin.Shell/Settings.cs @@ -7,7 +7,7 @@ namespace Wox.Plugin.Shell public Shell Shell { get; set; } = Shell.Cmd; public bool ReplaceWinR { get; set; } = true; public bool LeaveShellOpen { get; set; } - internal bool RunAsAdministrator { get; set; } = true; + public bool RunAsAdministrator { get; set; } = true; public Dictionary Count = new Dictionary();