Skip to main content

Gets the version information as read from the "AssemblyInfo.cs" file.

using System;
using System.Configuration;
using System.Diagnostics;
using System.Reflection;

namespace Helpers
{
    public class AssemblyVersionHelper
    {
        private const string environmentConfigKey = "environment";

        private AssemblyVersionHelper() { }

        static VersionHelper()
        {
            var assembly = Assembly.GetExecutingAssembly();
            var version = assembly.GetName().Version;

            // semantic versioning
            Version = string.Format("{0}.{1}.{2}",
                version.Major,
                version.Minor,
                version.Build
            );

            if (assembly.Location != null)
            {
                ProductVersion = FileVersionInfo.GetVersionInfo(
                    assembly.Location).ProductVersion;
            }

            AppEnvironment = ConfigurationManager.AppSettings[environmentConfigKey];

            ReleaseDate = new FileInfo(typeof(AssemblyVersionHelper).GetAssembly().Location).LastWriteTime;
        }

        static public string Version { get; private set; }
        static public string ProductVersion { get; private set; }
        static public string Environment { get; private set; }
        static public DateTime ReleaseDate { get; private set; }
    }
}