mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-09 04:37:30 +02:00
[PT Run] Run as user feature (#17283)
* run as different user * fix tests * Update src/modules/launcher/Plugins/Microsoft.Plugin.Shell/Main.cs * fix typo
This commit is contained in:
@@ -28,7 +28,7 @@ namespace Microsoft.Plugin.Indexer
|
||||
File,
|
||||
}
|
||||
|
||||
// Extensions for adding run as admin context menu item for applications
|
||||
// Extensions for adding run as admin and run as other user context menu item for applications
|
||||
private readonly string[] appExtensions = { ".exe", ".bat", ".appref-ms", ".lnk" };
|
||||
|
||||
public ContextMenuLoader(PluginInitContext context)
|
||||
@@ -53,6 +53,7 @@ namespace Microsoft.Plugin.Indexer
|
||||
if (CanFileBeRunAsAdmin(record.Path))
|
||||
{
|
||||
contextMenus.Add(CreateRunAsAdminContextMenu(record));
|
||||
contextMenus.Add(CreateRunAsUserContextMenu(record));
|
||||
}
|
||||
|
||||
contextMenus.Add(new ContextMenuResult
|
||||
@@ -145,6 +146,34 @@ namespace Microsoft.Plugin.Indexer
|
||||
};
|
||||
}
|
||||
|
||||
// Function to add the context menu item to run as admin
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "We want to keep the process alive, and instead log the exception message")]
|
||||
private static ContextMenuResult CreateRunAsUserContextMenu(SearchResult record)
|
||||
{
|
||||
return new ContextMenuResult
|
||||
{
|
||||
PluginName = Assembly.GetExecutingAssembly().GetName().Name,
|
||||
Title = Properties.Resources.Microsoft_plugin_indexer_run_as_user,
|
||||
Glyph = "\xE7EE",
|
||||
FontFamily = "Segoe MDL2 Assets",
|
||||
AcceleratorKey = Key.U,
|
||||
AcceleratorModifiers = ModifierKeys.Control | ModifierKeys.Shift,
|
||||
Action = _ =>
|
||||
{
|
||||
try
|
||||
{
|
||||
Task.Run(() => Helper.RunAsUser(record.Path));
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Exception($"Failed to run {record.Path} as different user, {e.Message}", e, MethodBase.GetCurrentMethod().DeclaringType);
|
||||
return false;
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// Function to test if the file can be run as admin
|
||||
private bool CanFileBeRunAsAdmin(string path)
|
||||
{
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace Microsoft.Plugin.Indexer.Properties {
|
||||
// class via a tool like ResGen or Visual Studio.
|
||||
// To add or remove a member, edit your .ResX file then rerun ResGen
|
||||
// with the /str option, or rebuild your VS project.
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")]
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")]
|
||||
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
|
||||
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
|
||||
public class Resources {
|
||||
@@ -61,7 +61,7 @@ namespace Microsoft.Plugin.Indexer.Properties {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Disable drive detection warning.
|
||||
/// Looks up a localized string similar to Disable non-indexed files warning.
|
||||
/// </summary>
|
||||
public static string disable_drive_detection_warning {
|
||||
get {
|
||||
@@ -186,6 +186,15 @@ namespace Microsoft.Plugin.Indexer.Properties {
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Run as different user (Ctrl+Shift+U).
|
||||
/// </summary>
|
||||
public static string Microsoft_plugin_indexer_run_as_user {
|
||||
get {
|
||||
return ResourceManager.GetString("Microsoft_plugin_indexer_run_as_user", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Search.
|
||||
/// </summary>
|
||||
|
||||
@@ -162,4 +162,7 @@
|
||||
<data name="disable_drive_detection_warning" xml:space="preserve">
|
||||
<value>Disable non-indexed files warning</value>
|
||||
</data>
|
||||
</root>
|
||||
<data name="Microsoft_plugin_indexer_run_as_user" xml:space="preserve">
|
||||
<value>Run as different user (Ctrl+Shift+U)</value>
|
||||
</data>
|
||||
</root>
|
||||
Reference in New Issue
Block a user