2017-10-08 5 views
0

"標準出力をどのように取得するか"を探していましたが、いくつかのチュートリアルが見つかりましたが、どれも "はい"とは思えませんでした。 exe "は私のために持っています。cmdからの出力の読み込み

はHERESに全体のコードは、エラー位置私は私のエラーを取得

public static string C(string arguments, bool b) 
    { 
     System.Diagnostics.Process process = new 
     System.Diagnostics.Process(); 
     System.Diagnostics.ProcessStartInfo startInfo = new 
     System.Diagnostics.ProcessStartInfo(); 
     startInfo.WindowStyle = 
     System.Diagnostics.ProcessWindowStyle.Hidden; 
     process.StartInfo.RedirectStandardOutput = true; 
     process.StartInfo.RedirectStandardInput = true; 
     process.StartInfo.UseShellExecute = false; 
     startInfo.FileName = "cmd.exe"; 
     startInfo.Arguments = arguments; 
     process.StartInfo = startInfo; 
     process.Start(); 

     string res = ""; 
     if (b) 
     { 
      StringBuilder q = new StringBuilder(); 
      while (!process.HasExited) 
      { 
       q.Append(process.StandardOutput.ReadToEnd()); 
      } 
      string r = q.ToString(); 
      res = r; 
     } 
     if(res == "" || res == null) 
     { 
      return "NO-RESULT"; 
     } 
     return res; 

    } 

のために下にスクロールします(と、System.InvalidOperationException:「をStandardOutがリダイレクトされていないか、プロセスがまだ開始されていない。」)

string res = ""; 
    StringBuilder q = new StringBuilder(); 
    while (!process.HasExited) 
    { 
     q.Append(process.StandardOutput.ReadToEnd()); // Right here 
    } 
    string r = q.ToString(); 
    res = r; 
+0

あなたは[この](https://stackoverflow.com/questions/1145969/processinfo-and-redirectstandardoutput)を試したことがありますか? –

答えて

0

あなたはその後、process.StartInfo上のいくつかのプロパティを設定し、基本的に、あなたがPRに設定何戻すprocess.StartInfostartInfoを割り当て、startInfoという名前ProcessStartInfoを作成しています明らかに。

あなたはstartInfoRedirectStandardOutputRedirectStandardInputUseShellExecuteを設定する必要があります。

System.Diagnostics.Process process = new System.Diagnostics.Process(); 
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); 
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; 
startInfo.RedirectStandardOutput = true; 
startInfo.RedirectStandardInput = true; 
startInfo.UseShellExecute = false; 
startInfo.FileName = "cmd.exe"; 
startInfo.Arguments = arguments; 
process.StartInfo = startInfo; 
process.Start(); 
+0

ありがとうございました!誰かが私のコードをよく読んで、作成者のLolを読むことができます。 – Doggo123445

関連する問題