-1

C# を使用して .exe プログラムを起動し、.exe から生成された cmd から値を読み取りたい

.exe は正常に起動しますが、値を読み取ることができません。

これは私のコードです:

ProcessStartInfo start = new ProcessStartInfo();

start.FileName = (@"D:\BSC\Thesis\Raphael_Thesis\smiledetector\bin\smiledetector.exe");
start.WorkingDirectory = @"D:\BSC\Thesis\Raphael_Thesis\smiledetector\bin\";
start.UseShellExecute = false;
start.RedirectStandardOutput = true;

//Start the process

using (Process process = Process.Start(start))
{
    // Read in all the text from the process with the StreamReader.
    using (StreamReader reader = process.StandardOutput)
    {
        string result = reader.ReadToEnd();
        textBox1.Text = result;
    }
}
4

3 に答える 3

1

Sam I Am が示しているように、using ブロックをドロップします。StreamReader

using (Process process = Process.Start(start))
{ 
    string result = process.StandardOutput.ReadToEnd();
    textBox1.Text = result;
}

ただし、呼び出し元のアプリケーションは、プロセスが完了してすべての出力が読み取られるまでブロックされることに注意してください。

于 2013-05-08T18:23:14.777 に答える
0

コードに奇妙な点は見当たりません。正常に動作するはずです。ドキュメントを見ると、リダイレクト方法の簡単な例が示されています。

 // Start the child process.
 using(Process p = new Process())
 {
   // Redirect the output stream of the child process.
   p.StartInfo.UseShellExecute = false;
   p.StartInfo.RedirectStandardOutput = true;
   p.StartInfo.FileName = @"D:\BSC\Thesis\Raphael_Thesis\smiledetector\bin\smiledetector.exe";
   p.WorkingDirectory = @"D:\BSC\Thesis\Raphael_Thesis\smiledetector\bin\";
   p.Start();
   // Do not wait for the child process to exit before
   // reading to the end of its redirected stream.
   // p.WaitForExit();
   // Read the output stream first and then wait.
   string output = p.StandardOutput.ReadToEnd();
   p.WaitForExit();
 }

ソース: Process.StandardOutput プロパティ

于 2013-05-08T18:33:55.760 に答える