6

シェルを介してコマンドを実行し、C#を使用して完全な出力を文字列として返すにはどうすればよいですか?

PHPのshell_exec()と同等です。

ありがとう、進んだ。

4

3 に答える 3

5

Processクラスを使用する

于 2011-06-21T20:59:52.540 に答える
5

Process.StandardOutputドキュメントのMSDNドキュメントに例が示されています

// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "Write500Lines.exe";
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();

標準エラーストリームも抽出することをお勧めします。

于 2011-06-21T21:07:22.923 に答える
1

プロセスクラスの標準出力標準エラー、およびOutputDataReceivedイベントと ErrorDataReceived受信イベントを確認します。

ここにCodeProjectの記事があり、役立つかもしれません。

于 2011-06-21T21:02:07.850 に答える