[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:
Heiko
2022-04-04 11:37:08 +02:00
committed by GitHub
parent 12282a8dc7
commit e444881320
17 changed files with 205 additions and 45 deletions

View File

@@ -101,7 +101,7 @@ namespace Wox.Infrastructure
{
FileName = path,
WorkingDirectory = Path.GetDirectoryName(path),
Verb = "runas",
Verb = "runAs",
UseShellExecute = true,
};
@@ -115,6 +115,28 @@ namespace Wox.Infrastructure
}
}
// Function to run as other user for context menu items
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Suppressing this to enable FxCop. We are logging the exception, and going forward general exceptions should not be caught")]
public static void RunAsUser(string path)
{
var info = new ProcessStartInfo
{
FileName = path,
WorkingDirectory = Path.GetDirectoryName(path),
Verb = "runAsUser",
UseShellExecute = true,
};
try
{
Process.Start(info);
}
catch (System.Exception ex)
{
Log.Exception($"Unable to Run {path} as different user : {ex.Message}", ex, MethodBase.GetCurrentMethod().DeclaringType);
}
}
public static Process OpenInConsole(string path)
{
var processStartInfo = new ProcessStartInfo
@@ -126,7 +148,7 @@ namespace Wox.Infrastructure
return Process.Start(processStartInfo);
}
public static bool OpenInShell(string path, string arguments = null, string workingDir = null, bool runAsAdmin = false, bool runWithHiddenWindow = false)
public static bool OpenInShell(string path, string arguments = null, string workingDir = null, ShellRunAsType runAs = ShellRunAsType.None, bool runWithHiddenWindow = false)
{
using (var process = new Process())
{
@@ -134,13 +156,16 @@ namespace Wox.Infrastructure
process.StartInfo.WorkingDirectory = string.IsNullOrWhiteSpace(workingDir) ? string.Empty : workingDir;
process.StartInfo.Arguments = string.IsNullOrWhiteSpace(arguments) ? string.Empty : arguments;
process.StartInfo.WindowStyle = runWithHiddenWindow ? ProcessWindowStyle.Hidden : ProcessWindowStyle.Normal;
process.StartInfo.UseShellExecute = true;
if (runAsAdmin)
if (runAs == ShellRunAsType.Administrator)
{
process.StartInfo.Verb = "RunAs";
}
process.StartInfo.UseShellExecute = true;
else if (runAs == ShellRunAsType.OtherUser)
{
process.StartInfo.Verb = "RunAsUser";
}
try
{
@@ -154,5 +179,12 @@ namespace Wox.Infrastructure
}
}
}
public enum ShellRunAsType
{
None,
Administrator,
OtherUser,
}
}
}