[英]StreamWrite (Interact) to another Application running within a C# Console App
I am writing a shell in C# using a Console App. 我正在使用控制台应用程序以C#编写Shell。 Using my Shell I would execute other applications such as ping.exe, takeown.exe etc just like you would with cmd or bash.
使用Shell可以执行其他应用程序,例如ping.exe,takeown.exe等,就像使用cmd或bash一样。
I have no problem in redirecting Standard Out from the other process to my Console App. 将Standard Out从其他进程重定向到我的控制台应用程序没有问题。 The issue is that I don't know how to redirect StardardIn properly so that I can interact with the running application .
问题是我不知道如何正确地重定向StardardIn,以便我可以与正在运行的应用程序进行交互 。 Ie say I need to enter "y" to confirm an action using cacls.exe.
也就是说,我需要输入“ y”以使用cacls.exe确认操作。
Here is my code for StandardOut: 这是我的StandardOut代码:
How can I write back to the App? 如何写回应用程序?
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();
You should write back the data to the app by using process.StandardInput.WriteLine(message)
and message should be a string. 您应该使用
process.StandardInput.WriteLine(message)
将数据写回到应用程序,并且message应该是字符串。
You can use Write()
or WriteLine()
您可以使用
Write()
或WriteLine()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.