2016-06-16 111 views
1

私はこの方法では、このコードを持っている:「StandardOutがリダイレクトされていないか、プロセスがまだ開始されていない」(Process.GetProcessById後)

int startApp() 
{ 
    ProcessStartInfo proc = new ProcessStartInfo(); 
    proc.FileName = "File.exe"; 
    proc.Arguments = "someArguments"; 
    proc.RedirectStandardInput = false; 
    proc.RedirectStandardOutput = true; 
    proc.UseShellExecute = false; 
    proc.CreateNoWindow = true; 

    Process p = Process.Start(proc); 
    Console.WriteLine(p.StandardOutput.ReadLine()); // Here it works! 

    return p.Id; 
} 

それから私は、Idがで返されるプロセスを使用してこのコードを実行します前の方法:

void readText(int processId) 
{ 
    Process p = Process.GetProcessById(processId); 
    Console.WriteLine(p.StandardOutput.ReadLine()); // Here does not work! 
} 

"StandardOut has not been redirected or the process hasn't started yet"というのは失敗します。

なぜ私はProcess.StandardOutputから読むことができないのですか?

+0

'process'オブジェクト全体を' startApp'から返し、 'readText'でそれを使用して、プロセスIDで再取得しないようにしてください。 – vcsjones

答えて

3

これはできません。プロセスを開始した場合はスタンダード{Input、Output、Error}を使用することができ、それを開始したプロセスオブジェクトからのみそれを使用することもできます。

プロセスを開始するために使用されるプロセスオブジェクトを、標準出力を読み取る必要があるコードで使用できるように配置します。

関連する問題