add run as different user option to context menu

This commit is contained in:
Jeremy Wu
2019-12-12 20:55:41 +11:00
parent 2e9acc1fa8
commit da798e7a5a
4 changed files with 54 additions and 22 deletions

View File

@@ -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<ProcessStartInfo, Process> 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<Result> LoadContextMenus(Result selectedResult)
{
return new List<Result>
var resultlist = new List<Result>
{
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;
}
}
}