Skip to content

Instantly share code, notes, and snippets.

@HeathHopkins
Created January 6, 2014 19:59
Show Gist options
  • Save HeathHopkins/8288865 to your computer and use it in GitHub Desktop.
Save HeathHopkins/8288865 to your computer and use it in GitHub Desktop.
Build date of current application. Must have wildcard in AssemblyVersion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using TestLibrary;
namespace AssemblyDateTest
{
public class Program
{
static void Main(string[] args)
{
var buildDate = GetBuildDate(Assembly.GetExecutingAssembly());
Console.WriteLine(buildDate.ToString("MM/dd/yyyy hh:mm tt (zz G\\MT)"));
}
public static DateTime GetBuildDate(System.Reflection.Assembly asm)
{
try
{
string version = asm.GetName().Version.ToString();
int days = int.Parse(version.Split(".".ToCharArray())[2]);
int seconds = int.Parse(version.Split(".".ToCharArray())[3]) * 2;
if (days < 1000)
throw new Exception();
return new DateTime(2000, 1, 1).AddDays(days).AddSeconds(seconds);
}
catch (Exception ex)
{
throw new Exception("Unable to parse assembly's version number to get the build date. Make sure AssemblyInfo has AssemblyVersion(\"1.0.*\")", ex);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment