Skip to content

Commit

Permalink
Default to getting the DLL's file version in AppVersionHelper.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed May 3, 2023
1 parent 757086b commit cfc4ddb
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions Shared/Utilities/AppVersionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,15 @@ public static string GetAppVersion(string defaultVersion = "1.0.0")
{
try
{
if (File.Exists(Environment.ProcessPath))
var filePath = Assembly.GetEntryAssembly()?.Location;
if (TryGetFileVersion(filePath, out var asmVersion))
{
var versionInfo = FileVersionInfo.GetVersionInfo(Environment.ProcessPath);
if (!string.IsNullOrEmpty(versionInfo.FileVersion))
{
return versionInfo.FileVersion;
}
return asmVersion;
}

var asmVersion = Assembly.GetEntryAssembly()?.GetName().Version;
if (asmVersion is not null && asmVersion > new Version(1, 0, 0))
if (TryGetFileVersion(Environment.ProcessPath, out var exeVersion))
{
return asmVersion.ToString();
return exeVersion;
}

return defaultVersion;
Expand All @@ -37,5 +33,28 @@ public static string GetAppVersion(string defaultVersion = "1.0.0")
return defaultVersion;
}
}

private static bool TryGetFileVersion(string filePath, out string version)
{
try
{
if (File.Exists(filePath))
{
var versionInfo = FileVersionInfo.GetVersionInfo(filePath);
if (Version.TryParse(versionInfo.FileVersion, out _))
{
version = versionInfo.FileVersion;
return true;
}
}
}
catch
{
// Ignored.
}

version = string.Empty;
return false;
}
}
}

0 comments on commit cfc4ddb

Please sign in to comment.