【发布时间】:2018-11-01 00:29:04
【问题描述】:
我正在尝试为提升的CMD 运行logman.exe,对于我尝试过的以下代码,
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = @"C:\Windows\System32\cmd.exe",
Arguments = "cmd /k logman.exe PerfCounterCustom | findstr \"Root\"",
Verb = "runas",
UseShellExecute = true,
}
};
try
{
proc.Start();
while (!proc.StandardOutput.EndOfStream)
{
string line = proc.StandardOutput.ReadLine();
}
Console.WriteLine("Successfully elevated!");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
它会给出错误输出,例如,
System.InvalidOperationException: StandardOut has not been redirected or the process hasn't started yet.
at System.Diagnostics.Process.get_StandardOutput()
2 个问题,
- 当我运行应用程序
exe时,它显示 2 个CMD窗口,第一个显示错误,第二个显示参数"cmd /k logman.exe PerfCounterCustom | findstr \"Root\""[根路径] 的结果
如何禁用同时显示两个窗口?
- 为什么会出现此错误?
【问题讨论】:
-
为什么不直接打电话给logman呢?为什么要调用 cmd.exe?
-
使用
UseShellExecute = true,您正在使用shell (cmd) 运行您的命令。你的命令启动了另一个 shell,所以你有 2 个窗口。 -
感谢@Evk,有没有办法将第二个窗口(根路径)的结果放入变量中?我想我需要将此输出移动到第一个窗口,但我无法做到
-
我认为你根本不需要任何外壳。只需单独运行 logman 命令(甚至不使用 findstr),不使用 shell,然后手动解析它的重定向输出。
-
我的问题,我需要提升它以运行 logman 命令,这就是为什么我以管理员身份打开其他 shell
标签: c# cmd process elevated-privileges