110,538
社区成员
发帖
与我相关
我的任务
分享
using (Process p = new Process())
{
p.StartInfo.FileName = System.IO.Path.Combine(Application.StartupPath, "exe//x64.exe");
p.StartInfo.Arguments = args;
//p.StartInfo.FileName = "c://windows//system32//cmd.exe";
//p.StartInfo.Arguments = "/c ping www.baidu.com";
p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
p.StartInfo.RedirectStandardInput = true; //接受来自调用程序的输入信息
p.StartInfo.RedirectStandardOutput = true; //由调用程序获取输出信息
p.StartInfo.RedirectStandardError = true; //重定向标准错误输出
p.StartInfo.CreateNoWindow = false; //不显示程序窗口
p.OutputDataReceived += new DataReceivedEventHandler(GethOutputHandler);
p.ErrorDataReceived += new DataReceivedEventHandler(GethOutputHandler);
p.Start();//启动程序
p.BeginOutputReadLine();
p.BeginErrorReadLine();
}
private void GethOutputHandler(object sender, DataReceivedEventArgs dataReceived)
{
if (dataReceived.Data == null)
return;
Console.WriteLine(dataReceived.Data);
}
static void Main(string[] args)
{
Console.WriteLine("这里是x64.exe,即将输出该程序的输入参数");
foreach(string s in args)
{
Console.WriteLine("参数:"+s);
}
//other codes
}
2、调用程序,winform
private void button1_Click(object sender, EventArgs e)
{
string output = string.Empty;
using(Process p=new Process())
{
p.StartInfo.FileName = "x64.exe";
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.Arguments = textBox1.Text;
p.Start();
output = p.StandardOutput.ReadToEnd();
}
MessageBox.Show(output);
}
测试
textBox1输入
winform程序将控制台x64.exe的输出,重定向到winform