cmd.exe到C#:交互式命令

时间:2014-08-19 19:17:33

标签: .net process cmd system.diagnostics

我必须在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 ]:

有什么想法吗?

0 个答案:

没有答案