common helper for execute shell process from run plugins (#9538)

This commit is contained in:
Davide Giacometti
2021-02-23 09:53:08 +01:00
committed by GitHub
parent 20a922ce21
commit 571bceb386
17 changed files with 77 additions and 128 deletions

View File

@@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO.Abstractions;
using System.Reflection;
@@ -117,5 +118,33 @@ namespace Wox.Infrastructure
return Process.Start(processStartInfo);
}
public static bool OpenInShell(string path, string arguments = null, string workingDir = null, bool runAsAdmin = false)
{
using (var process = new Process())
{
process.StartInfo.FileName = path;
process.StartInfo.WorkingDirectory = string.IsNullOrWhiteSpace(workingDir) ? string.Empty : workingDir;
process.StartInfo.Arguments = string.IsNullOrWhiteSpace(arguments) ? string.Empty : arguments;
if (runAsAdmin)
{
process.StartInfo.Verb = "RunAs";
}
process.StartInfo.UseShellExecute = true;
try
{
process.Start();
return true;
}
catch (Win32Exception ex)
{
Log.Exception($"Unable to open {path}: {ex.Message}", ex, MethodBase.GetCurrentMethod().DeclaringType);
return false;
}
}
}
}
}