■No19072 (鶏唐揚 さん) に返信 マルチスレッドでなく、という要件を満たすかどうかわかりませんが (非同期読み取りは内部的にマルチスレッドなので)、こんなコードでできました。 構文的には C# 3.0、フレームワーク的にも .NET Framework 2.0 のを使っちゃってますけど。 using (Process p = new Process()) { p.StartInfo.FileName = "ping.exe"; p.StartInfo.Arguments = "127.0.0.1"; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; p.StartInfo.CreateNoWindow = true; p.StartInfo.UseShellExecute = false; p.OutputDataReceived += (sender, e) => { if( ! string.IsNullOrEmpty(e.Data) ) { Console.WriteLine(e.Data); } }; p.Start(); p.BeginOutputReadLine(); p.WaitForExit(); } Console.WriteLine(); Console.WriteLine("Complete"); が、DataReceivedEventArgs.Data には時折空文字列が来るようで、 string.IsNullOrEmpty を外すと、数行の空行が出てしまいますが、 排除すると、コマンドプロンプトから叩いた時の出るべき空行も排除してしまうので、 そのままの出力を再現することはできませんでした。 他にも方法はあるかと思います。
- Child Tree -