File tree Expand file tree Collapse file tree 1 file changed +28
-9
lines changed Expand file tree Collapse file tree 1 file changed +28
-9
lines changed Original file line number Diff line number Diff line change @@ -15,19 +15,15 @@ public static string GetAppVersion(string defaultVersion = "1.0.0")
15
15
{
16
16
try
17
17
{
18
- if ( File . Exists ( Environment . ProcessPath ) )
18
+ var filePath = Assembly . GetEntryAssembly ( ) ? . Location ;
19
+ if ( TryGetFileVersion ( filePath , out var asmVersion ) )
19
20
{
20
- var versionInfo = FileVersionInfo . GetVersionInfo ( Environment . ProcessPath ) ;
21
- if ( ! string . IsNullOrEmpty ( versionInfo . FileVersion ) )
22
- {
23
- return versionInfo . FileVersion ;
24
- }
21
+ return asmVersion ;
25
22
}
26
23
27
- var asmVersion = Assembly . GetEntryAssembly ( ) ? . GetName ( ) . Version ;
28
- if ( asmVersion is not null && asmVersion > new Version ( 1 , 0 , 0 ) )
24
+ if ( TryGetFileVersion ( Environment . ProcessPath , out var exeVersion ) )
29
25
{
30
- return asmVersion . ToString ( ) ;
26
+ return exeVersion ;
31
27
}
32
28
33
29
return defaultVersion ;
@@ -37,5 +33,28 @@ public static string GetAppVersion(string defaultVersion = "1.0.0")
37
33
return defaultVersion ;
38
34
}
39
35
}
36
+
37
+ private static bool TryGetFileVersion ( string filePath , out string version )
38
+ {
39
+ try
40
+ {
41
+ if ( File . Exists ( filePath ) )
42
+ {
43
+ var versionInfo = FileVersionInfo . GetVersionInfo ( filePath ) ;
44
+ if ( Version . TryParse ( versionInfo . FileVersion , out _ ) )
45
+ {
46
+ version = versionInfo . FileVersion ;
47
+ return true ;
48
+ }
49
+ }
50
+ }
51
+ catch
52
+ {
53
+ // Ignored.
54
+ }
55
+
56
+ version = string . Empty ;
57
+ return false ;
58
+ }
40
59
}
41
60
}
You can’t perform that action at this time.
0 commit comments