-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVersionInfo.cs
More file actions
84 lines (70 loc) · 2.85 KB
/
VersionInfo.cs
File metadata and controls
84 lines (70 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System.Linq;
using System.Reflection;
/// <summary>
/// Provides version information for the application
/// </summary>
public static class VersionInfo
{
private static readonly Lazy<string> _buildDate = new(() => GetBuildDate());
private static readonly Lazy<string> _gitCommitHash = new(() => GetGitCommitHash());
/// <summary>
/// Gets the full version string (e.g., "1.0.0" or "1.0.0-beta")
/// </summary>
public static string Version => GetVersion();
/// <summary>
/// Gets the assembly version
/// </summary>
public static string AssemblyVersion => Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "Unknown";
/// <summary>
/// Gets the file version
/// </summary>
public static string FileVersion => GetFileVersion();
/// <summary>
/// Gets the informational version (includes version suffix if present)
/// </summary>
public static string InformationalVersion => GetInformationalVersion();
/// <summary>
/// Gets the build date and time (UTC)
/// </summary>
public static string BuildDate => _buildDate.Value;
/// <summary>
/// Gets the git commit hash
/// </summary>
public static string GitCommitHash => _gitCommitHash.Value;
/// <summary>
/// Gets the full version string with all available information
/// </summary>
public static string FullVersion => $"{Version} (Build: {BuildDate}, Commit: {GitCommitHash})";
private static string GetVersion()
{
var assembly = Assembly.GetExecutingAssembly();
var version = assembly.GetName().Version;
return version != null ? $"{version.Major}.{version.Minor}.{version.Build}" : "Unknown";
}
private static string GetFileVersion()
{
var assembly = Assembly.GetExecutingAssembly();
var fileVersionAttr = assembly.GetCustomAttribute<AssemblyFileVersionAttribute>();
return fileVersionAttr?.Version ?? "Unknown";
}
private static string GetInformationalVersion()
{
var assembly = Assembly.GetExecutingAssembly();
var infoVersionAttr = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
return infoVersionAttr?.InformationalVersion ?? Version;
}
private static string GetBuildDate()
{
var assembly = Assembly.GetExecutingAssembly();
var metadata = assembly.GetCustomAttributes<AssemblyMetadataAttribute>();
var buildDateAttr = metadata.FirstOrDefault(m => m.Key == "BuildDate");
return buildDateAttr?.Value ?? "Unknown";
}
private static string GetGitCommitHash()
{
var assembly = Assembly.GetExecutingAssembly();
var metadata = assembly.GetCustomAttributes<AssemblyMetadataAttribute>();
var commitAttr = metadata.FirstOrDefault(m => m.Key == "GitCommitHash");
return commitAttr?.Value ?? "Unknown";
}
}