mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 03:07:04 +02:00
add run as different user option to context menu
This commit is contained in:
BIN
Plugins/Wox.Plugin.Shell/Images/user.png
Normal file
BIN
Plugins/Wox.Plugin.Shell/Images/user.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 64 KiB |
@@ -5,6 +5,7 @@
|
||||
<system:String x:Key="wox_plugin_cmd_relace_winr">Replace Win+R</system:String>
|
||||
<system:String x:Key="wox_plugin_cmd_leave_cmd_open">Do not close Command Prompt after command execution</system:String>
|
||||
<system:String x:Key="wox_plugin_cmd_always_run_as_administrator">Always run as administrator</system:String>
|
||||
<system:String x:Key="wox_plugin_cmd_run_as_different_user">Run as different user</system:String>
|
||||
<system:String x:Key="wox_plugin_cmd_plugin_name">Shell</system:String>
|
||||
<system:String x:Key="wox_plugin_cmd_plugin_description">Allows to execute system commands from Wox. Commands should start with ></system:String>
|
||||
<system:String x:Key="wox_plugin_cmd_cmd_has_been_executed_times">this command has been executed {0} times</system:String>
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,6 +70,9 @@
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="Images\user.png">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
<Content Include="Languages\en.xaml">
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
<SubType>Designer</SubType>
|
||||
|
||||
Reference in New Issue
Block a user