StreamWrite(Interact)到在C#控制台应用程序中运行的另一个应用程序

时间:2017-02-09 10:26:33

标签: c# bash cmd console-application

我正在使用Console App在C#中编写shell。使用我的Shell我将执行其他应用程序,如ping.exe,takeown.exe等,就像使用cmd或bash一样。

将标准输出从其他进程重定向到我的控制台应用程序时没有问题。问题是我不知道如何正确地重定向StardardIn,以便我可以与正在运行的应用程序进行交互。即我需要输入“y”以使用cacls.exe确认操作。

以下是我的StandardOut代码:

如何回复应用程序?

Process process = new Process();

process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = cmd;
process.StartInfo.Arguments = Arguments;

process.OutputDataReceived += new DataReceivedEventHandler(
        (s, e) =>
             {
                  Console.WriteLine(e.Data);
             }
        );
process.ErrorDataReceived += new DataReceivedEventHandler((s, e) => { Console.WriteLine(e.Data); });

process.Start();
process.BeginOutputReadLine();
process.WaitForExit();

1 个答案:

答案 0 :(得分:0)

您应该使用process.StandardInput.WriteLine(message)将数据写回应用程序 和消息应该是一个字符串。

您可以使用Write()WriteLine()