C# 使用ProcessStartInfo調用exe獲取不到重定向數據的解決方案


emmmmm,最近在研究WFDB工具箱,C語言寫的,無奈本人C語言功底不夠,只想直接拿來用,於是打算通過ProcessStartInfo來調取編譯出來的exe程序獲取輸出。

一開始就打算偷懶,從園子里的前輩blog上偷來部分代碼,和着自己寫的代碼差不多就寫了以下調取程序的代碼:

 1         /// <summary>
 2         /// 調用Exe核心代碼
 3         /// </summary>
 4         /// <param name="exeFileName"></param>
 5         /// <param name="args"></param>
 6         public void RunExe(string exeFileName, string args = "")
 7         {
 8             try
 9             {
10 
11                 Process p = new Process();
12 
13                 p.StartInfo = new ProcessStartInfo(exeFileName, args);
14 
15                 p.StartInfo.Arguments = args;
16                 //p.StartInfo.WorkingDirectory = @"C:\MinGW\msys\1.0\home\61125\wfdb-10.6.1\build\bin";
17                 p.StartInfo.UseShellExecute = false;
18 
19                 p.StartInfo.RedirectStandardOutput = true;
20 
21                 //p.StartInfo.RedirectStandardInput = true;
22 
23                 p.StartInfo.RedirectStandardError = true;
24 
25                 p.StartInfo.CreateNoWindow = false;
26                 //綁定事件
27                 p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
28                 p.ErrorDataReceived += P_ErrorDataReceived;
29 
30                 p.Start();
31                 p.BeginOutputReadLine();//開始讀取輸出數據
32                 p.WaitForExit();
33                 p.Close();
34             }
35             catch (Exception e)
36             {
37                 Console.WriteLine(e);
38                 throw;
39             }
40         }

然后使用時發現了問題,按照wfdb 中app的設計,直接調取exe是會彈出help內容的,可是我自己調用卻不行。

無奈自己就搭建C環境,MinGw配合Visual Code配置了一把,踩了好多坑,才在windows環境下跑起了wfdb並能夠調試。具體坑就不談了。(有意願的小伙伴可以和我溝通交流一下~)

研究了C代碼發現異常輸出都是通過

1 fprintf(stderr, "xxxxx")

此類形式輸出的,搜索一下,查看了前輩的文章(https://www.cnblogs.com/tshua/p/5730658.html)發現輸出流是有講究的,不是全部通過Output通道發送的

於是困擾了我兩天的問題解決方案如下:

 1         /// <summary>
 2         /// 調用Exe核心代碼
 3         /// </summary>
 4         /// <param name="exeFileName"></param>
 5         /// <param name="args"></param>
 6         public void RunExe(string exeFileName, string args = "")
 7         {
 8             try
 9             {
10 
11                 Process p = new Process();
12 
13                 p.StartInfo = new ProcessStartInfo(exeFileName, args);
14 
15                 p.StartInfo.Arguments = args;
16                 //p.StartInfo.WorkingDirectory = @"C:\MinGW\msys\1.0\home\61125\wfdb-10.6.1\build\bin";
17                 p.StartInfo.UseShellExecute = false;
18 
19                 p.StartInfo.RedirectStandardOutput = true;
20 
21                 //p.StartInfo.RedirectStandardInput = true;
22 
23                 p.StartInfo.RedirectStandardError = true;
24 
25                 p.StartInfo.CreateNoWindow = false;
26                 //綁定事件
27                 p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
28                 p.ErrorDataReceived += P_ErrorDataReceived;
29 
30                 p.Start();
31                 p.BeginOutputReadLine();//開始讀取輸出數據
32                 p.BeginErrorReadLine();//開始讀取錯誤數據,重要!
33                 p.WaitForExit();
34                 p.Close();
35             }
36             catch (Exception e)
37             {
38                 Console.WriteLine(e);
39                 throw;
40             }
41         }

加入一行關鍵的

p.BeginErrorReadLine();//開始讀取錯誤數據,重要!

就解決了問題。

emmmm。。(lll¬ω¬)

僅以此篇隨筆防止其他小伙伴和我一樣走彎路≡(▔﹏▔)≡

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM