Last active
December 29, 2016 13:39
-
-
Save dampee/38c7e22bff230b69399c7c9300266be8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CustomVersionProvider : IVersionProvider | |
{ | |
private Lazy<string> _productVersion = new Lazy<string>(() => | |
{ | |
var assembly = Assembly.GetExecutingAssembly(); | |
var assemblyVersion = assembly.GetName().Version; | |
var productVersion = string.Format("{0}.{1}.{2}", assemblyVersion.Major, assemblyVersion.Minor, assemblyVersion.Build); | |
// additional info | |
var dateFormat = "dd.MM.yyyyTHH:mm:ss"; | |
productVersion += string.Format(" (built: {0}) (LastWriteTime: {1})", | |
GetLinkerTime(assembly).ToString(dateFormat), | |
File.GetLastWriteTime(assembly.Location).ToString(dateFormat)); | |
return productVersion; | |
}); | |
public string GetVersion() | |
{ | |
return _productVersion.Value; | |
} | |
private static DateTime GetLinkerTime(Assembly assembly, TimeZoneInfo target = null) | |
{ | |
var filePath = assembly.Location; | |
const int c_PeHeaderOffset = 60; | |
const int c_LinkerTimestampOffset = 8; | |
var buffer = new byte[2048]; | |
using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) | |
stream.Read(buffer, 0, 2048); | |
var offset = BitConverter.ToInt32(buffer, c_PeHeaderOffset); | |
var secondsSince1970 = BitConverter.ToInt32(buffer, offset + c_LinkerTimestampOffset); | |
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); | |
var linkTimeUtc = epoch.AddSeconds(secondsSince1970); | |
var tz = target ?? TimeZoneInfo.Local; | |
var localTime = TimeZoneInfo.ConvertTimeFromUtc(linkTimeUtc, tz); | |
return localTime; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IVersionProvider | |
{ | |
string GetVersion(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment