zoukankan      html  css  js  c++  java
  • C# 调用外部程序,并获取输出和错误信息

    1. 同步模式
    public void exec(string exePath, string parameters)
            {
                System.Diagnostics.ProcessStartInfo psi =
       new System.Diagnostics.ProcessStartInfo();
                psi.RedirectStandardOutput = true;
                psi.RedirectStandardError = true;
                psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                psi.UseShellExecute = false;
                psi.FileName = exePath;
                psi.Arguments = parameters;
                System.Diagnostics.Process process = System.Diagnostics.Process.Start(psi);
                System.IO.StreamReader outputStreamReader = process.StandardOutput;
                System.IO.StreamReader errStreamReader = process.StandardError;
                process.WaitForExit(2000);
                if (process.HasExited)
                {
                    string output = outputStreamReader.ReadToEnd();
                    string error = errStreamReader.ReadToEnd();
                    MessageBox.Show(output);
                    MessageBox.Show(error);
                }
    
            }

    2.异步模式

     public void exec(string exePath, string parameters)
            {
                Process process = new System.Diagnostics.Process();
                process.StartInfo.FileName = exePath;
                process.StartInfo.Arguments = parameters;
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.RedirectStandardOutput = true;
                process.Start();
                process.BeginOutputReadLine();
                process.OutputDataReceived += new DataReceivedEventHandler(processOutputDataReceived);
            }
    
            private void processOutputDataReceived(object sender, DataReceivedEventArgs e)
            {
                MessageBox.Show(e.Data); 
            }


  • 相关阅读:
    裸裸的spfa~嘿嘿嘿!
    睡前1小时数学系列之-整除
    拓扑排序1.奖金
    拓扑排序
    SCU 1095运送物资(最短路)
    POJ1158 城市交通Traffic lights IOI 1999 (最短路)
    POI0109 POD (最短路)
    HN0I2000最优乘车 (最短路变形)
    FOJ1205 小鼠迷宫问题 (BFD+递推)
    CJOI 05新年好 (最短路+枚举)
  • 原文地址:https://www.cnblogs.com/jerry1999/p/3677328.html
Copyright © 2011-2022 走看看