私はこのコードでcmdコマンドを実行しましたが、ver、cd ...のようなコマンドの大部分でうまく動作しますが、netstatやdir
などのコマンドで多数のファイルがあるフォルダでは、コマンドは終了しました。C#からcmdコマンドの終了を正しく検出できませんか?
私はタイムアウトを使用することができますが、それは、netstatコマンドの終了を検出することができない理由を私は知りたい
string results="";
string command = @"dir c:\windows";
ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + command);
procStartInfo.RedirectStandardInput = true;
procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardError = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
Process proc = new Process();
proc.StartInfo = procStartInfo;
proc.Start();
results = proc.StandardOutput.ReadToEnd(); //<-Move here
proc.WaitForExit();
//results = proc.StandardOutput.ReadToEnd(); //Moved
Console.WriteLine(results);
修正:
私は結果を読んで移動し(結果= proc.StandardOutput.ReadToEnd() ;)それは問題を解決する。私は問題を再現することができませんでしたあなたのコードでは
netstatはexeです。 cmdで起動する必要はありません。たぶんそれはあなたが出力を読むために読むのに役立ちます – gsharp