Elevating privileges doesn't work with UseShellExecute=false

后端 未结 3 1064
被撕碎了的回忆
被撕碎了的回忆 2020-11-28 14:26

I want to start a child process (indeed the same, console app) with elevated privileges but with hidden window.

I do next:

var info = new ProcessStar         


        
3条回答
  •  天涯浪人
    2020-11-28 15:02

    In my case, it was ok to get the outputs once the elevated child process is done. Here's the solution I came up. It uses a temporary file :

    var output = Path.GetTempFileName();
    var process = Process.Start(new ProcessStartInfo
    {
        FileName = "cmd",
        Arguments = "/c echo I'm an admin > " + output, // redirect to temp file
        Verb = "runas", // UAC prompt
        UseShellExecute = true,
    });
    process.WaitForExit();
    string res = File.ReadAllText(output);
    // do something with the output
    File.Delete(output);
    

提交回复
热议问题