diff --git a/Plugins/Wox.Plugin.Shell/Images/user.png b/Plugins/Wox.Plugin.Shell/Images/user.png new file mode 100644 index 0000000000..2d45c1ee91 Binary files /dev/null and b/Plugins/Wox.Plugin.Shell/Images/user.png differ diff --git a/Plugins/Wox.Plugin.Shell/Languages/en.xaml b/Plugins/Wox.Plugin.Shell/Languages/en.xaml index 6f56e83f58..0d0a41b3f1 100644 --- a/Plugins/Wox.Plugin.Shell/Languages/en.xaml +++ b/Plugins/Wox.Plugin.Shell/Languages/en.xaml @@ -5,6 +5,7 @@ Replace Win+R Do not close Command Prompt after command execution Always run as administrator + Run as different user 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 323cfb5cd3..1a5174c4e4 100644 --- a/Plugins/Wox.Plugin.Shell/Main.cs +++ b/Plugins/Wox.Plugin.Shell/Main.cs @@ -1,8 +1,10 @@ -using System; +using System; using System.Collections.Generic; +using System.ComponentModel; using System.Diagnostics; using System.IO; using System.Linq; +using System.Threading.Tasks; using System.Windows; using WindowsInput; using WindowsInput.Native; @@ -84,7 +86,7 @@ namespace Wox.Plugin.Shell IcoPath = Image, Action = c => { - Execute(m); + Execute(Process.Start, PrepareProcessStartInfo(m)); return true; } })); @@ -117,7 +119,7 @@ namespace Wox.Plugin.Shell IcoPath = Image, Action = c => { - Execute(m.Key); + Execute(Process.Start, PrepareProcessStartInfo(m.Key)); return true; } }; @@ -136,7 +138,7 @@ namespace Wox.Plugin.Shell IcoPath = Image, Action = c => { - Execute(cmd); + Execute(Process.Start, PrepareProcessStartInfo(cmd)); return true; } }; @@ -154,14 +156,14 @@ namespace Wox.Plugin.Shell IcoPath = Image, Action = c => { - Execute(m.Key); + Execute(Process.Start, PrepareProcessStartInfo(m.Key)); return true; } }).Take(5); return history.ToList(); } - private void Execute(string command, bool runAsAdministrator = false) + private ProcessStartInfo PrepareProcessStartInfo(string command, bool runAsAdministrator = false) { command = command.Trim(); command = Environment.ExpandEnvironmentVariables(command); @@ -212,19 +214,33 @@ namespace Wox.Plugin.Shell } else { - return; + throw new NotImplementedException(); } info.UseShellExecute = true; + _settings.AddCmdHistory(command); + + return info; + } + + private void Execute(Func startProcess,ProcessStartInfo info) + { try { - Process.Start(info); - _settings.AddCmdHistory(command); + startProcess(info); } catch (FileNotFoundException e) { - MessageBox.Show($"Command not found: {e.Message}"); + var name = "Plugin: Shell"; + var message = $"Command not found: {e.Message}"; + _context.API.ShowMsg(name, message); + } + catch(Win32Exception e) + { + var name = "Plugin: Shell"; + var message = $"Error running the command: {e.Message}"; + _context.API.ShowMsg(name, message); } } @@ -306,19 +322,31 @@ namespace Wox.Plugin.Shell public List LoadContextMenus(Result selectedResult) { - return new List + var resultlist = new List { - new Result - { - Title = _context.API.GetTranslation("wox_plugin_cmd_run_as_administrator"), - Action = c => - { - Execute(selectedResult.Title, true); - return true; - }, - IcoPath = Image - } - }; + new Result + { + Title = _context.API.GetTranslation("wox_plugin_cmd_run_as_different_user"), + Action = c => + { + Task.Run(() =>Execute(ShellCommand.RunAsDifferentUser, PrepareProcessStartInfo(selectedResult.Title))); + return true; + }, + IcoPath = "Images/user.png" + }, + new Result + { + Title = _context.API.GetTranslation("wox_plugin_cmd_run_as_administrator"), + Action = c => + { + Execute(Process.Start, PrepareProcessStartInfo(selectedResult.Title, true)); + return true; + }, + IcoPath = Image + } + }; + + return resultlist; } } } diff --git a/Plugins/Wox.Plugin.Shell/Wox.Plugin.Shell.csproj b/Plugins/Wox.Plugin.Shell/Wox.Plugin.Shell.csproj index 2291f78bb8..270a55c3b4 100644 --- a/Plugins/Wox.Plugin.Shell/Wox.Plugin.Shell.csproj +++ b/Plugins/Wox.Plugin.Shell/Wox.Plugin.Shell.csproj @@ -70,6 +70,9 @@ + + Always + MSBuild:Compile Designer