2

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?

user584018
  • 10,186
  • 15
  • 74
  • 160

1 Answers1

3

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.

Rich Moss
  • 2,195
  • 1
  • 13
  • 18