How to get the version number of a .NET assembly from the command line?

To get the version number of a .NET binary from the command line, you are able to use Powershell in some cases.

The following command can be used if Powerhell is an option :

powershell (Get-Command "path-to-your.dll").FileVersionInfo.FileVersion

If Powershell is not an option but you are free to add a small utility to your work environment, the C# equivalent would be similar to:

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

namespace VersionInfo
{
	public class Program
	{
		protected static void Main(string[] args)
		{
			if (args.Length != 1)
			{
				Console.WriteLine($"Usage: {AppDomain.CurrentDomain.FriendlyName} file.dll");
				return;
			}

			string file = args[0];

			if (!System.IO.File.Exists(file))
			{
				Console.WriteLine($"File {file} does not exist or access denied.");
				return;
			}

			// Open assembly
			Assembly assembly = Assembly.LoadFrom(file);
			Console.WriteLine(FileVersionInfo.GetVersionInfo(assembly.Location).FileVersion);
		}
	}
}

 

When using a build server and for whatever reason, you need access to the version number from a batch,  you would be able to use one of the following commands:

powershell (Get-Command "path-to-your.dll").FileVersionInfo.FileVersion > version.txt

or

versioninfo "path-to-your.dll" > version.txt

and read it back with:

set /p V=<version.txt
echo Version: %V%


Profile picture
Gideon Bakx
Updated on: Friday, October 6, 2023 8:10:46 PM
C# certified software engineer living in Calgary, Canada.
C# Powershell