C# StandardOutput重定向 问题

  转自:http://bbs.csdn.net/topics/190051920#new_post     

this.process = new Process();
        this.process.StartInfo.CreateNoWindow = true;
        this.process.StartInfo.UseShellExecute = false;
        this.process.StartInfo.FileName = fileName;
        this.process.StartInfo.RedirectStandardError = false;
        this.process.StartInfo.RedirectStandardInput = true;
        this.process.StartInfo.RedirectStandardOutput = true; 
        this.process.Start(); 
        StreamWriter sw = this.process.StandardInput;
        sw.Write(test);
        sw.Close();
        string output = this.process.StandardOutput.ReadToEnd();
        this.process.WaitForExit();
        this.process.Close();
中间的 test、fileName 等变量是我先前定义好的
现在有个问题是:
当我的输出在1000行的时候是完全正常的
但当输出的行为2000或以上时就阻塞了

也就是当输入过多时
string output = this.process.StandardOutput.ReadToEnd();
程序就会在上面一行阻塞

请大家忙帮
给一个解决方案
不胜感激

补充一下
是输出过多时

就是我知道输出会比较多时(一般是1000行以上)
就会阻塞

我也想过可能是输入太多
我把
        this.process.StartInfo.RedirectStandardOutput = true; 
该成
        this.process.StartInfo.RedirectStandardOutput = false;

无论输入多少测试数据
程序都正常

是不是StandardOutput有大小限制
如果解决
谢谢 

这些我都明白
我说了在数据少的时候运行是正常的
不是完全没有运行

现在还发现一个问题
如果输入超过好几千行
也会出现阻塞

谢谢阿
(再次说下,我已经成功运行了,就是数据输入或输出过多时出现了问题,期待。。。。。。)

 

缓冲区确实是有大小限制的.而且如果缓冲区满了的话,系统会阻塞等待缓冲区排出适当的数据.
解决办法就是在缓冲区还没有满之前,将缓冲区中的数据输出到某个地方.
另外,一个workaround的方法就是通过重定向符号(>, >>这样的符号,通过调用批处理文件来替代.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,针对你的问题,以下是关于 C# 输入输出重定向的方法一:同步 Process 的解答。 在 C# 中,可以使用 Process 类来执行外部程序,并且可以通过重定向输入和输出流来实现与外部程序的交互。具体步骤如下: 1. 创建 ProcessStartInfo 对象,设置 FileName 属性为要执行的外部程序的路径,设置 RedirectStandardInput 和 RedirectStandardOutput 属性为 true,表示重定向输入和输出流。 2. 创建 Process 对象,设置 StartInfo 属性为上述 ProcessStartInfo 对象,然后调用 Start 方法启动进程。 3. 使用 Process 对象的 StandardInput 属性获取重定向的输入流,使用 WriteLine 方法向外部程序写入数据。 4. 使用 Process 对象的 StandardOutput 属性获取重定向的输出流,使用 ReadLine 方法从外部程序读取数据。 以下是一个简单的示例代码: ``` using System; using System.Diagnostics; class Program { static void Main() { var startInfo = new ProcessStartInfo { FileName = "外部程序路径", RedirectStandardInput = true, RedirectStandardOutput = true, UseShellExecute = false }; var process = new Process { StartInfo = startInfo }; process.Start(); var standardInput = process.StandardInput; var standardOutput = process.StandardOutput; standardInput.WriteLine("要写入的数据"); var output = standardOutput.ReadLine(); Console.WriteLine(output); process.WaitForExit(); } } ``` 以上就是 C# 输入输出重定向方法一:同步 Process 的解答。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值