简体   繁体   English

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

[英]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.

相关问题 使C#控制台应用程序与wpf交互 - Making a C# console application interact with wpf C#中的条件Streamwrite - Conditional Streamwrite in C# 从 c# 控制台应用程序中运行将 2 个文件引用为 arguments 的 .exe 应用程序 - Running an .exe application that references 2 files as arguments from within a c# console app 从excel运行c#控制台应用程序并等待应用程序完成 - running c# console application from excel and wait for app to finish 通过新的C#交互式控制台交互/操纵应用程序的值 - Interact/manipulate Values of Application through the new C# Interactive Console C# 应用程序在控制台中使用另一个应用程序 - C# application to use another application in Console 控制台应用程序中的C#子命令 - C# Subcommands within console application WM 6.5 C#控制台应用程序,在另一个应用程序具有焦点的情况下在后台运行时捕获击键 - WM 6.5 C# console application, capturing keystrokes while running in the background while another application has focus 运行 C# 控制台应用程序作为 Windows 服务 - Running a C# console application as a Windows service 在C#控制台应用程序中运行C ++控制台应用程序 - Running a c++ console app inside a C# console app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM