Not able to send commands to cmd.exe process

為{幸葍}努か 提交于 2019-11-29 18:49:26

I could never get it to work with synchronous reads of stdout without closing stdin, but it does work with async reading for stdout/stderr. No need to pass in /c, you only do that when passing in a command through the arguments; you are not doing this though, you are sending the command directly to the input.

var p = new Process()
{
    StartInfo = {
    CreateNoWindow = false,
    UseShellExecute = false,
    RedirectStandardError = true,
    RedirectStandardInput = true,
    RedirectStandardOutput = true,
    FileName = @"cmd.exe"}
};
p.OutputDataReceived += (sender, args1) => Console.WriteLine(args1.Data);
p.ErrorDataReceived += (sender, args1) => Console.WriteLine(args1.Data);
p.Start();
p.BeginOutputReadLine();
p.StandardInput.WriteLine("dir");
p.StandardInput.WriteLine("cd e:");
p.WaitForExit();

Console.WriteLine("Done");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!