[英]Read/Write data to Console generated by exe from windows form application
I have a simple c# windowsform application which calls an exe .The exe inturn opens a console. 我有一个简单的c#windowsform应用程序,它调用exe。exe依次打开一个控制台。 Is it possible to read and write data to the CONSOLE generated by the exe. 是否可以读取和写入数据到exe生成的控制台。 I want to send data from my windows forms application 我想从Windows窗体应用程序发送数据
If your executable is creating the console by using a Process instance that runs 'cmd.exe' then you can redirect the input and output of the console by setting the StartupInfo of the Process... 如果您的可执行文件通过使用运行'cmd.exe'的Process实例创建控制台,则可以通过设置Process的StartupInfo来重定向控制台的输入和输出...
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
...then you can hook into an event the occurs when user enters console input... ...然后您可以将用户输入控制台输入时发生的事件与事件联系起来...
p.OutputDataReceived += new .DataReceivedEventHandler(ConsoleOutputHandler);
void ConsoleOutputHandler(object sender, DataReceivedEventArgs rec)
{
// do something with the data
}
You can pass arguments using Process.startInfo()
. 您可以使用Process.startInfo()
传递参数。 And obtaining parameters should be done according to your requirement (may be reading from textbox etc.) 并应根据您的要求获取参数(可以从文本框等中读取)
// Extracting parameter from form
string para1=textbox1.Text;
// State the program to be start; PATH is the path to program .exe
ProcessStartInfo startInfo = new ProcessStartInfo(PATH);
// Passing arguments :para1 extracted from textbox (string type)
startInfo.Arguments = para1
// Starting process
Process exec= Process.Start(startInfo);
// optionally waiting for execution
exec.WaitForExit();
Also this can be done in single line as follows, 另外,也可以按以下方式在一行中完成此操作,
// Simply define program to execute and pass a and b parameters
Process.Start(PATH,para1);
Note : path is a string like "program1.exe"
or full path to your program 注意:path是类似于"program1.exe"
的字符串或程序的完整路径
Additionally : You are not LIMITED to one parameter 另外:您不限于一个参数
// Extracting parameter from form
string para1=textbox1.Text;
string para2=textbox2.Text;
Process.Start(PATH,para1+" "+para2); // Pass 2 parameters (using + string concat)
You can send many parameters simply separating them by spaces.. When you need to include space in parameter simply escape them using '\\'
您可以发送许多参数,只需将它们用空格分隔即可。当需要在参数中包含空格时,只需使用'\\'
其转义即可'\\'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.