Launch process that needs to trigger UAC in netcor

2019-08-02 08:34发布

I often need to launch external processes so I wrote a convenient method to easily do that. One of these processes needs to trigger UAC in order to ask the user for their permission. I did some research and found that setting the Verb property of the ProcessStartInfo object to runas in addition to setting the UseShellExecute to true should do the trick.

private static void StartProcess(string fileName, string arguments, bool elevated)
{
    var start = new ProcessStartInfo
    {
        UseShellExecute = false,
        CreateNoWindow = true,
        Arguments = arguments,
        FileName = fileName
    };

    if (elevated) 
    {
        start.Verb = "runas";
        start.UseShellExecute = true;
    }

    int exitCode = 0;
    using (var proc = new Process { StartInfo = start })
    {
        proc.Start();
        proc.WaitForExit();
        exitCode = proc.ExitCode;
    }

    if (exitCode != 0)
    {
        var message = string.Format(
            "Error {0} executing {1} {2}",
            exitCode,
            start.FileName,
            start.Arguments);
        throw new InvalidOperationException(message);
    }
}

However, the Verb property is not available in netcore therefore I can't figure out how to achieve the same result. Any suggestion?

标签: c# .net-core
0条回答
登录 后发表回答