Исходный код вики System.Diagnostics.Process

Версия 2.1 от Alexandr Fokin на 2022/06/06 14:40

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