简体   繁体   English

动词=“ runas”未以提升的方式运行

[英]Verb = “runas” not run as elevated

As discussed in other post, I came to know that Verb = "runas" works as elevated. 正如在其他文章中所讨论的那样,我知道Verb =“ runas”可以提升。

I need to run "logman.exe" arguments with Elevated privileged. 我需要以特权提升运行“ logman.exe”参数。 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. 注意-当我在EXE上运行时,以Admin右键单击,我得到输出。

What changes required so that I can make Elevated through code in C# and output? 需要进行哪些更改,以便可以通过C#中的代码进行“提升”并输出?

Process.Start() can use the OS or Shell (Explorer.exe) to spawn a new process, but only the Shell will prompt for elevation. Process.Start()可以使用OS或命令行管理程序(Explorer.exe)生成新进程,但只有命令行管理程序会提示您提升权限。

Thus you have to specify UseShellExecute=true in ProcessStartInfo, according to this answer: processstartinfo-verb-runas-not-working 因此,您必须根据以下答案在ProcessStartInfo中指定UseShellExecute = true: processstartinfo-verb-runas-not-working

UseShellExecute=false will allow you to capture Standard Output and Standard Error messages. UseShellExecute = false将允许您捕获标准输出和标准错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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