@@ -7,46 +7,50 @@ public static class ProcessHelper
77 {
88 public static string Run ( string path , string args , out int error , out string errorMessage )
99 {
10- using ( var process = new Process ( ) )
10+ return RunFrom ( null , path , args , out error , out errorMessage ) ;
11+ }
12+
13+ public static string RunFrom ( string workingDir , string path , string args , out int error , out string errorMessage )
14+ {
15+ using var process = new Process ( ) ;
16+ process . StartInfo . WorkingDirectory = workingDir ;
17+ process . StartInfo . FileName = path ;
18+ process . StartInfo . Arguments = args ;
19+ process . StartInfo . UseShellExecute = false ;
20+ process . StartInfo . RedirectStandardOutput = true ;
21+ process . StartInfo . RedirectStandardError = true ;
22+
23+ var reterror = new StringBuilder ( ) ;
24+ var retout = new StringBuilder ( ) ;
25+ process . OutputDataReceived += ( sender , outargs ) =>
26+ {
27+ if ( string . IsNullOrEmpty ( outargs . Data ) )
28+ return ;
29+
30+ if ( retout . Length > 0 )
31+ retout . AppendLine ( ) ;
32+ retout . Append ( outargs . Data ) ;
33+ } ;
34+ process . ErrorDataReceived += ( sender , errargs ) =>
1135 {
12- process . StartInfo . FileName = path ;
13- process . StartInfo . Arguments = args ;
14- process . StartInfo . UseShellExecute = false ;
15- process . StartInfo . RedirectStandardOutput = true ;
16- process . StartInfo . RedirectStandardError = true ;
36+ if ( string . IsNullOrEmpty ( errargs . Data ) )
37+ return ;
1738
18- var reterror = new StringBuilder ( ) ;
19- var retout = new StringBuilder ( ) ;
20- process . OutputDataReceived += ( sender , outargs ) =>
21- {
22- if ( ! string . IsNullOrEmpty ( outargs . Data ) )
23- {
24- if ( retout . Length > 0 )
25- retout . AppendLine ( ) ;
26- retout . Append ( outargs . Data ) ;
27- }
28- } ;
29- process . ErrorDataReceived += ( sender , errargs ) =>
30- {
31- if ( ! string . IsNullOrEmpty ( errargs . Data ) )
32- {
33- if ( reterror . Length > 0 )
34- reterror . AppendLine ( ) ;
35- reterror . Append ( errargs . Data ) ;
36- }
37- } ;
39+ if ( reterror . Length > 0 )
40+ reterror . AppendLine ( ) ;
41+ reterror . Append ( errargs . Data ) ;
42+ } ;
3843
39- process . Start ( ) ;
40- process . BeginOutputReadLine ( ) ;
41- process . BeginErrorReadLine ( ) ;
42- process . WaitForExit ( ) ;
43- process . CancelOutputRead ( ) ;
44- process . CancelErrorRead ( ) ;
44+ process . Start ( ) ;
45+ process . BeginOutputReadLine ( ) ;
46+ process . BeginErrorReadLine ( ) ;
47+ process . WaitForExit ( ) ;
48+ process . CancelOutputRead ( ) ;
49+ process . CancelErrorRead ( ) ;
4550
46- error = process . ExitCode ;
47- errorMessage = reterror . ToString ( ) ;
48- return retout . ToString ( ) ;
49- }
51+ error = process . ExitCode ;
52+ errorMessage = reterror . ToString ( ) ;
53+ return retout . ToString ( ) ;
5054 }
5155 }
5256}
0 commit comments