Add run as administrator to Shell plugin settings

This commit is contained in:
Jeremy Wu 2019-12-10 08:23:34 +11:00
parent 1fd31d83bf
commit 201c26f7c8
4 changed files with 19 additions and 26 deletions

View File

@ -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"
},

View File

@ -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
{

View File

@ -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<string, int> Count = new Dictionary<string, int>();
public void AddCmdHistory(string cmdName)

View File

@ -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;