@@ -27,14 +27,22 @@ public static Process Start(ProcessStartInfo startInfo)
27
27
}
28
28
catch ( Win32Exception exception )
29
29
{
30
- // NOTE: https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382.aspx
31
- // -- @asbjornu
32
- if ( exception . NativeErrorCode == 2 )
30
+ switch ( ( NativeErrorCode ) exception . NativeErrorCode )
33
31
{
34
- throw new FileNotFoundException ( string . Format ( "The executable file '{0}' could not be found." ,
35
- startInfo . FileName ) ,
36
- startInfo . FileName ,
37
- exception ) ;
32
+ case NativeErrorCode . Success :
33
+ // Success is not a failure.
34
+ break ;
35
+
36
+ case NativeErrorCode . FileNotFound :
37
+ throw new FileNotFoundException ( string . Format ( "The executable file '{0}' could not be found." ,
38
+ startInfo . FileName ) ,
39
+ startInfo . FileName ,
40
+ exception ) ;
41
+
42
+ case NativeErrorCode . PathNotFound :
43
+ throw new DirectoryNotFoundException ( string . Format ( "The path to the executable file '{0}' could not be found." ,
44
+ startInfo . FileName ) ,
45
+ exception ) ;
38
46
}
39
47
40
48
throw ;
@@ -47,19 +55,24 @@ public static Process Start(ProcessStartInfo startInfo)
47
55
process . PriorityClass = ProcessPriorityClass . Idle ;
48
56
}
49
57
}
50
- catch ( Win32Exception exception )
58
+ catch
51
59
{
52
-
53
- // NOTE: It seems like in some situations, setting the priority class will throw an exception
60
+ // NOTE: It seems like in some situations, setting the priority class will throw a Win32Exception
54
61
// with the error code set to "Success", which I think we can safely interpret as a success and
55
62
// not an exception.
56
- // See https://travis-ci.org/GitTools/GitVersion/jobs/171288284#L2026
57
- // and https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382.aspx
63
+ //
64
+ // See: https://travis-ci.org/GitTools/GitVersion/jobs/171288284#L2026
65
+ // And: https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382.aspx
66
+ //
67
+ // There's also the case where the process might be killed before we try to adjust its priority
68
+ // class, in which case it will throw an InvalidOperationException. What we ideally should do
69
+ // is start the process in a "suspended" state, adjust the priority class, then resume it, but
70
+ // that's not possible in pure .NET.
71
+ //
72
+ // See: https://travis-ci.org/GitTools/GitVersion/jobs/166709203#L2278
73
+ // And: http://www.codeproject.com/Articles/230005/Launch-a-process-suspended
74
+ //
58
75
// -- @asbjornu
59
- if ( exception . NativeErrorCode == 0 )
60
- {
61
- throw ;
62
- }
63
76
}
64
77
}
65
78
}
@@ -142,6 +155,17 @@ public static int Run(Action<string> output, Action<string> errorOutput, TextRea
142
155
}
143
156
}
144
157
158
+ /// <summary>
159
+ /// System error codes.
160
+ /// See: https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382.aspx
161
+ /// </summary>
162
+ private enum NativeErrorCode
163
+ {
164
+ Success = 0x0 ,
165
+ FileNotFound = 0x2 ,
166
+ PathNotFound = 0x3
167
+ }
168
+
145
169
[ Flags ]
146
170
public enum ErrorModes
147
171
{
0 commit comments