C#学习四之输入输出重定向

在窗体程序的创建中,可以给一个button添加一个事件让它去调用其它程序,并将其他程序的输出返回到窗口上,避免了控制台应用难看的界面,以下是实例:

// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = @"calCRC.exe"; //别忘了把exe放到窗口程序的bin的debug目录下,这是相对路径
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
p.StandardInput.WriteLine(textBox1.Text);      //重定向标准输入,将会作为calCRC的输入
string output = p.StandardOutput.ReadToEnd();  //重定向标准输出,将会把calCRC的输出存下来 

string error = p.StandardError.ReadToEnd();    //重定向标准错误

if(output != null)
textBox2.Text = output;
else
{
textBox2.Text = error;
}

转载于:https://www.cnblogs.com/clownice/p/4396351.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值