9

How can I print in my log the version of the program that is running? In other words, can I access AssemblyFileVersion using Console.WriteLine?

Thanks Tony

tony
  • 823
  • 2
  • 16
  • 25

3 Answers3

9

It looks like something like this would work:

public static string Version
{
    get
    {
        Assembly asm = Assembly.GetExecutingAssembly();
        FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(asm.Location);
        return String.Format("{0}.{1}", fvi.ProductMajorPart, fvi.ProductMinorPart);
    }
}

From another post on SO.

Community
  • 1
  • 1
Alex Zylman
  • 920
  • 1
  • 11
  • 20
5
// Get the version of the current application.
Assembly assem = Assembly.GetExecutingAssembly();
AssemblyName assemName = assem.GetName();
Version ver = assemName.Version;
Console.WriteLine("{0}, Version {1}", assemName.Name, ver.ToString());

More on MSDN:

Version Class

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
0

FileVersionInfo.GetVersionInfo(asm.Location) doesn't work for embedded assemblies (e.g. using Fody's Costura to ship a single EXE instead of the executable and all of it's dependent assemblies). In which case, the following works as a fallback:

var assembly = Assembly.GetExecutingAssembly();
var fileVersionAttribute = assembly.CustomAttributes.FirstOrDefault(ca => ca.AttributeType == typeof(AssemblyFileVersionAttribute));
if (fileVersionAttribute != null && fileVersionAttribute.ConstructorArguments.Any())
    return fileVersionAttribute.ConstructorArguments[0].ToString().Replace("\"","");
return string.Empty;
Joseph Simpson
  • 4,054
  • 1
  • 24
  • 28