我必须在cmd.exe
中执行一些命令,它运行良好,但交互式命令不起作用。例如,在正常sc
窗口中执行cmd.exe
时,在输出结束时,您会得到:
Would you like to see help for the QUERY and QUERYEX commands? [ y | n ]:
如果按y
,您可以获得更多输出。好吧,当从C#执行此操作时,我无法通过这样的命令。即使发送y
字符也不会使屏幕进度,它会永远卡在那里。
这是我到目前为止所尝试的:
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardInput = true,
RedirectStandardError = true,
CreateNoWindow = true,
WorkingDirectory = "C:\\"
}
};
proc.Start();
Task.Run(() => ShowInConsoleAsync(proc.StandardOutput));
Task.Run(() => ShowInConsoleAsync(proc.StandardError));
proc.StandardInput.WriteLine("sc");
proc.StandardInput.Flush();
Thread.Sleep(1000);
proc.StandardInput.Write("y");
proc.StandardInput.Flush();
Console.ReadKey(true);
但它被困在这一行:
Would you like to see help for the QUERY and QUERYEX commands? [ y | n ]:
有什么想法吗?