简体   繁体   中英

Verb = “runas” not run as elevated

As discussed in other post, I came to know that Verb = "runas" works as elevated.

I need to run "logman.exe" arguments with Elevated privileged. With below code, I am not getting any output,

try
        {
            var process = new Process()
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName = "logman.exe",
                    Arguments = "PerfCounterCustom",
                    Verb = "runas",
                    RedirectStandardOutput = true,
                    CreateNoWindow = true,
                }
            };

            process.Start();

            string lineData;
            while ((lineData = process.StandardOutput.ReadLine()) != null)
            {
                if (lineData.Contains("Root Path:"))
                {
                    Console.WriteLine(lineData.Trim());
                }
            }

            process.WaitForExit();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }

Note - When I am running above EXE, right click as Admin, I m getting the output.

What changes required so that I can make Elevated through code in C# and output?

Process.Start() can use the OS or Shell (Explorer.exe) to spawn a new process, but only the Shell will prompt for elevation.

Thus you have to specify UseShellExecute=true in ProcessStartInfo, according to this answer: processstartinfo-verb-runas-not-working

UseShellExecute=false will allow you to capture Standard Output and Standard Error messages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM