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"), Title = api.GetTranslation("wox_plugin_program_run_as_administrator"),
Action = _ => Action = _ =>
{ {
return Main.StartProcess(ShellCommand.SetCMDRunAsAdministrator(FullPath, ParentDirectory)); return Main.StartProcess(ShellCommand.SetProcessStartInfo(FullPath, ParentDirectory));
}, },
IcoPath = "Images/cmd.png" IcoPath = "Images/cmd.png"
}, },

View File

@@ -9,6 +9,7 @@ using WindowsInput.Native;
using Wox.Infrastructure.Hotkey; using Wox.Infrastructure.Hotkey;
using Wox.Infrastructure.Logger; using Wox.Infrastructure.Logger;
using Wox.Infrastructure.Storage; using Wox.Infrastructure.Storage;
using Wox.Plugin.SharedCommands;
using Application = System.Windows.Application; using Application = System.Windows.Application;
using Control = System.Windows.Controls.Control; using Control = System.Windows.Controls.Control;
using Keys = System.Windows.Forms.Keys; using Keys = System.Windows.Forms.Keys;
@@ -164,16 +165,15 @@ namespace Wox.Plugin.Shell
{ {
command = command.Trim(); command = command.Trim();
command = Environment.ExpandEnvironmentVariables(command); command = Environment.ExpandEnvironmentVariables(command);
var workingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
var runAsAdministratorArg = !runAsAdministrator && !_settings.RunAsAdministrator ? "" : "runas";
ProcessStartInfo info; ProcessStartInfo info;
if (_settings.Shell == Shell.Cmd) if (_settings.Shell == Shell.Cmd)
{ {
var arguments = _settings.LeaveShellOpen ? $"/k \"{command}\"" : $"/c \"{command}\" & pause"; var arguments = _settings.LeaveShellOpen ? $"/k \"{command}\"" : $"/c \"{command}\" & pause";
info = new ProcessStartInfo
{ info = ShellCommand.SetProcessStartInfo("cmd.exe", workingDirectory, arguments, runAsAdministratorArg);
FileName = "cmd.exe",
Arguments = arguments,
};
} }
else if (_settings.Shell == Shell.Powershell) else if (_settings.Shell == Shell.Powershell)
{ {
@@ -186,11 +186,8 @@ namespace Wox.Plugin.Shell
{ {
arguments = $"\"{command} ; Read-Host -Prompt \\\"Press Enter to continue\\\"\""; arguments = $"\"{command} ; Read-Host -Prompt \\\"Press Enter to continue\\\"\"";
} }
info = new ProcessStartInfo
{ info = ShellCommand.SetProcessStartInfo("powershell.exe", workingDirectory, arguments, runAsAdministratorArg);
FileName = "powershell.exe",
Arguments = arguments
};
} }
else if (_settings.Shell == Shell.RunCommand) else if (_settings.Shell == Shell.RunCommand)
{ {
@@ -200,21 +197,17 @@ namespace Wox.Plugin.Shell
var filename = parts[0]; var filename = parts[0];
if (ExistInPath(filename)) if (ExistInPath(filename))
{ {
var arguemtns = parts[1]; var arguments = parts[1];
info = new ProcessStartInfo info = ShellCommand.SetProcessStartInfo(filename, workingDirectory, arguments, runAsAdministratorArg);
{
FileName = filename,
Arguments = arguemtns
};
} }
else else
{ {
info = new ProcessStartInfo(command); info = ShellCommand.SetProcessStartInfo(command, verb: runAsAdministratorArg);
} }
} }
else else
{ {
info = new ProcessStartInfo(command); info = ShellCommand.SetProcessStartInfo(command, verb: runAsAdministratorArg);
} }
} }
else else
@@ -222,10 +215,7 @@ namespace Wox.Plugin.Shell
return; return;
} }
info.UseShellExecute = true; info.UseShellExecute = true;
info.WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
info.Verb = runAsAdministrator ? "runas" : "";
try try
{ {

View File

@@ -7,6 +7,8 @@ namespace Wox.Plugin.Shell
public Shell Shell { get; set; } = Shell.Cmd; public Shell Shell { get; set; } = Shell.Cmd;
public bool ReplaceWinR { get; set; } = true; public bool ReplaceWinR { get; set; } = true;
public bool LeaveShellOpen { get; set; } public bool LeaveShellOpen { get; set; }
internal bool RunAsAdministrator { get; set; } = true;
public Dictionary<string, int> Count = new Dictionary<string, int>(); public Dictionary<string, int> Count = new Dictionary<string, int>();
public void AddCmdHistory(string cmdName) public void AddCmdHistory(string cmdName)

View File

@@ -9,13 +9,14 @@ namespace Wox.Plugin.SharedCommands
{ {
public static class ShellCommand 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 var info = new ProcessStartInfo
{ {
FileName = fullPath, FileName = fileName,
WorkingDirectory = parentDirectory, WorkingDirectory = workingDirectory,
Verb = "runas" Arguments = arguments,
Verb = verb
}; };
return info; return info;