zoukankan      html  css  js  c++  java
  • C#输入输出重定向 方法一:同步process

    使用前需加上:   using System.Diagnostics; 

    同步重定向代码如下:

    第一步:确定必要成分

    1 Process process = new Process();
    2 process.StartInfo.UseShellExecute = false;   // 是否使用外壳程序 
    3 process.StartInfo.CreateNoWindow = true;   //是否在新窗口中启动该进程的值 
    4 process.StartInfo.RedirectStandardInput = true;  // 重定向输入流 
    5 process.StartInfo.RedirectStandardOutput= true;  //重定向输出流 
    6 process.StartInfo.RedirectStandardError= true;  //重定向错误流 

    需要注意的是:若要使用 StandardOutput,必须将 :

    ProcessStartInfo.UseShellExecute = false;

    ProcessStartInfo.RedirectStandardOutput = true;

    否则,读取StandardOutput 流时将引发异常;

    接下来出入执行文件的路径:

    1  process.StartInfo.FileName = "";//待输入的执行文件路径

    第二步:运行新进程

    1 process.Start();
    2 process.StandardInput.WriteLine(textBox1.Text); //从textBox1中输入信息到输入流
    3 string output = process.StandardOutput.ReadToEnd();//获取exe处理之后的输出信息到output
    4 string error = process.StandardError.ReadToEnd(); //获取错误信息到error
    5 process.Close(); //close进程

    这样就可以将原本exe输出到黑框重定向到该程序的output中;

    将错误输出重定向到error中,用于之后的操作;

    值得注意的是:所执行文件的当前目录在 项目\bin\Debug下

  • 相关阅读:
    常吃二十种降血脂食物,三高不再缠身
    员工能力要从“人海战术”转向“精兵强将”
    企业家必备的4项核心能力
    优秀管理者在哪些方面超乎常人
    高血压 降压方法
    教育视频
    吉他和弦 学习
    spoj 375 QTREE
    hihocoder #1260 : String Problem I
    codeforces 282E. Sausage Maximization Trie
  • 原文地址:https://www.cnblogs.com/Elson8080/p/4398937.html
Copyright © 2011-2022 走看看