Исходный код вики System.Diagnostics.Process
Версия 2.3 от Alexandr Fokin на 2022/06/06 15:42
Скрыть последних авторов
| author | version | line-number | content |
|---|---|---|---|
| |
1.3 | 1 | Process.start: how to get the output? |
| 2 | https://stackoverflow.com/questions/4291912/process-start-how-to-get-the-output | ||
| |
2.1 | 3 | |
| 4 | ---- | ||
| 5 | |||
| 6 | Template | ||
| 7 | |||
| 8 | {{code language="c#"}} | ||
| 9 | ProcessStartInfo info = new ProcessStartInfo(); | ||
| 10 | // fill info ... | ||
| |
2.2 | 11 | info.RedirectStandardOutput = true; |
| 12 | info.RedirectStandardError = true; | ||
| |
2.1 | 13 | |
| 14 | //token.ThrowIfCancellationRequested(); | ||
| |
2.3 | 15 | using Process process = Process.Start(info); |
| |
2.1 | 16 | try |
| 17 | { | ||
| 18 | await process.WaitForExitAsync(token); | ||
| 19 | } | ||
| 20 | catch (OperationCanceledException) | ||
| 21 | { | ||
| 22 | process.Kill(true); | ||
| 23 | await process.WaitForExitAsync(); | ||
| 24 | throw; | ||
| 25 | } | ||
| 26 | |||
| 27 | var outputString = await process.StandardOutput.ReadToEndAsync(); | ||
| 28 | var errorString = await process.StandardError.ReadToEndAsync(); | ||
| 29 | |||
| 30 | if (process.ExitCode == 0) | ||
| 31 | { | ||
| 32 | //Successe | ||
| 33 | } | ||
| 34 | else | ||
| 35 | { | ||
| 36 | //Error | ||
| 37 | } | ||
| 38 | {{/code}} |