Elevating privileges doesn't work with UseShellExecute=false

后端 未结 3 1054
被撕碎了的回忆
被撕碎了的回忆 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 14:51

    ProcessStartInfo.Verb will only have an effect if the process is started by ShellExecuteEx(). Which requires UseShellExecute = true. Redirecting I/O and hiding the window can only work if the process is started by CreateProcess(). Which requires UseShellExecute = false.

    Well, that's why it doesn't work. Not sure if forbidding to start a hidden process that bypasses UAC was intentional. Probably. Very probably.

    Check this Q+A for the manifest you need to display the UAC elevation prompt.

    0 讨论(0)
  • 2020-11-28 14:55

    Check this answer.

    This seems to provide a workaround. But I recommend to try other methods like Named Pipes when you have access to source code of the child process.

    0 讨论(0)
  • 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);
    
    0 讨论(0)
提交回复
热议问题