Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

C# get program version

private static DateTime GetLinkerTimestampUtc(Assembly assembly)
{
  var location = assembly.Location;
  return GetLinkerTimestampUtc(location);
}

private static DateTime GetLinkerTimestampUtc(string filePath)
{
  const int peHeaderOffset = 60;
  const int linkerTimestampOffset = 8;
  var bytes = new byte[2048];

  using (var file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
  {
    file.Read(bytes, 0, bytes.Length);
  }

  var headerPos = BitConverter.ToInt32(bytes, peHeaderOffset);
  var secondsSince1970 = BitConverter.ToInt32(bytes, headerPos + linkerTimestampOffset);
  var dt = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  return  DateTime.Parse(dt.AddSeconds(secondsSince1970).ToString()).ToLocalTime();
}

public static String GetReleaseVersion()
{
  return String.Format("{0} {1}", Assembly.GetExecutingAssembly().GetName().Version.ToString(), GetLinkerTimestampUtc(Assembly.GetExecutingAssembly()));
}
 
PREVIOUS NEXT
Tagged: #program #version
ADD COMMENT
Topic
Name
7+4 =