Programmatically get the version number of a DLL

C#.Net

C# Problem Overview


Is it possible to get the version number programmatically from any .NET DLL?

If yes, how?

C# Solutions


Solution 1 - C#

This works if the dll is .net or Win32. Reflection methods only work if the dll is .net. Also, if you use reflection, you have the overhead of loading the whole dll into memory. The below method does not load the assembly into memory.

// Get the file version.
FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(@"C:\MyAssembly.dll");

// Print the file name and version number.
Console.WriteLine("File: " + myFileVersionInfo.FileDescription + '\n' +
                  "Version number: " + myFileVersionInfo.FileVersion);

From: http://msdn.microsoft.com/en-us/library/system.diagnostics.fileversioninfo.fileversion.aspx

original source

Solution 2 - C#

Assembly assembly = Assembly.LoadFrom("MyAssembly.dll");
Version ver = assembly.GetName().Version;

Important: It should be noted that this is not the best answer to the original question. Don't forget to read more on this page.

Solution 3 - C#

First of all, there are two possible 'versions' that you might be interested in:

  • Windows filesystem file version, applicable to all executable files

  • Assembly build version, which is embedded in a .NET assembly by the compiler (obviously only applicable to .NET assembly dll and exe files)

In the former case, you should use Ben Anderson's answer; in the latter case, use AssemblyName.GetAssemblyName(@"c:\path\to\file.dll").Version, or Tataro's answer, in case the assembly is referenced by your code.

Note that you can ignore all the answers that use .Load()/.LoadFrom() methods, since these actually load the assembly in the current AppDomain - which is analogous to cutting down a tree to see how old it is.

Solution 4 - C#

Here's a nice way using a bit of reflection to get a version of a DLL containing a particular class:

var ver = System.Reflection.Assembly.GetAssembly(typeof(!Class!)).GetName().Version;

Just replace !Class! with the name of a class which is defined in the DLL you wish to get the version of.

This is my preferred method because if I move the DLLs around for different deploys I don't have to change the filepath.

Solution 5 - C#

To get it for the assembly that was started (winform, console app, etc...)

using System.Reflection;
...
Assembly.GetEntryAssembly().GetName().Version

Solution 6 - C#

Kris, your version works great when needing to load the assembly from the actual DLL file (and if the DLL is there!), however, one will get a much unwanted error if the DLL is EMBEDDED (i.e., not a file but an embedded DLL).

The other thing is, if one uses a versioning scheme with something like "1.2012.0508.0101", when one gets the version string you'll actually get "1.2012.518.101"; note the missing zeros.

So, here's a few extra functions to get the version of a DLL (embedded or from the DLL file):

    public static System.Reflection.Assembly GetAssembly(string pAssemblyName)
	{
		System.Reflection.Assembly tMyAssembly = null;

		if (string.IsNullOrEmpty(pAssemblyName)) { return tMyAssembly; }
		tMyAssembly = GetAssemblyEmbedded(pAssemblyName);
		if (tMyAssembly == null) { GetAssemblyDLL(pAssemblyName); }

		return tMyAssembly;
	}//System.Reflection.Assembly GetAssemblyEmbedded(string pAssemblyDisplayName)


	public static System.Reflection.Assembly GetAssemblyEmbedded(string pAssemblyDisplayName)
	{
		System.Reflection.Assembly tMyAssembly = null;

		if(string.IsNullOrEmpty(pAssemblyDisplayName)) { return tMyAssembly; }
		try //try #a
		{
			tMyAssembly = System.Reflection.Assembly.Load(pAssemblyDisplayName);
		}// try #a
		catch (Exception ex)
		{
			string m = ex.Message;
		}// try #a
		return tMyAssembly;
	}//System.Reflection.Assembly GetAssemblyEmbedded(string pAssemblyDisplayName)


	public static System.Reflection.Assembly GetAssemblyDLL(string pAssemblyNameDLL)
	{
		System.Reflection.Assembly tMyAssembly = null;

		if (string.IsNullOrEmpty(pAssemblyNameDLL)) { return tMyAssembly; }
		try //try #a
		{
			if (!pAssemblyNameDLL.ToLower().EndsWith(".dll")) { pAssemblyNameDLL += ".dll"; }
			tMyAssembly = System.Reflection.Assembly.LoadFrom(pAssemblyNameDLL);
		}// try #a
		catch (Exception ex)
		{
			string m = ex.Message;
		}// try #a
		return tMyAssembly;
	}//System.Reflection.Assembly GetAssemblyFile(string pAssemblyNameDLL)


    public static string GetVersionStringFromAssembly(string pAssemblyDisplayName)
	{
		string tVersion = "Unknown";
		System.Reflection.Assembly tMyAssembly = null;

		tMyAssembly = GetAssembly(pAssemblyDisplayName);
		if (tMyAssembly == null) { return tVersion; }
		tVersion = GetVersionString(tMyAssembly.GetName().Version.ToString());
		return tVersion;
	}//string GetVersionStringFromAssemblyEmbedded(string pAssemblyDisplayName)


	public static string GetVersionString(Version pVersion)
	{
		string tVersion = "Unknown";
		if (pVersion == null) { return tVersion; }
		tVersion = GetVersionString(pVersion.ToString());
		return tVersion;
	}//string GetVersionString(Version pVersion)


	public static string GetVersionString(string pVersionString)
	{
		string tVersion = "Unknown";
		string[] aVersion;

		if (string.IsNullOrEmpty(pVersionString)) { return tVersion; }
		aVersion = pVersionString.Split('.');
		if (aVersion.Length > 0) { tVersion = aVersion[0]; }
		if (aVersion.Length > 1) { tVersion += "." + aVersion[1]; }
		if (aVersion.Length > 2) { tVersion += "." + aVersion[2].PadLeft(4, '0'); }
		if (aVersion.Length > 3) { tVersion += "." + aVersion[3].PadLeft(4, '0'); }

		return tVersion;
	}//string GetVersionString(Version pVersion)


	public static string GetVersionStringFromAssemblyEmbedded(string pAssemblyDisplayName)
	{
		string tVersion = "Unknown";
		System.Reflection.Assembly tMyAssembly = null;

		tMyAssembly = GetAssemblyEmbedded(pAssemblyDisplayName);
		if (tMyAssembly == null) { return tVersion; }
		tVersion = GetVersionString(tMyAssembly.GetName().Version.ToString());
		return tVersion;
	}//string GetVersionStringFromAssemblyEmbedded(string pAssemblyDisplayName)


	public static string GetVersionStringFromAssemblyDLL(string pAssemblyDisplayName)
	{
		string tVersion = "Unknown";
		System.Reflection.Assembly tMyAssembly = null;

		tMyAssembly = GetAssemblyDLL(pAssemblyDisplayName);
		if (tMyAssembly == null) { return tVersion; }
		tVersion = GetVersionString(tMyAssembly.GetName().Version.ToString());
		return tVersion;
	}//string GetVersionStringFromAssemblyEmbedded(string pAssemblyDisplayName)

Solution 7 - C#

Answer by @Ben proved to be useful for me. But I needed to check the product version as it was the main increment happening in my software and followed semantic versioning.

myFileVersionInfo.ProductVersion

This method met my expectations

Update: Instead of explicitly mentioning dll path in program (as needed in production version), we can get product version using Assembly.

Assembly assembly = Assembly.GetExecutingAssembly();
FileVersionInfo fileVersionInfo =FileVersionInfo.GetVersionInfo(assembly.Location); 
string ProdVersion= fileVersionInfo.ProductVersion;

Solution 8 - C#

var versionAttrib = new AssemblyName(Assembly.GetExecutingAssembly().FullName);

Solution 9 - C#

You can use System.Reflection.Assembly.Load*() methods and then grab their AssemblyInfo.

Solution 10 - C#

While the original question may not have been specific to a web service, here is a complete testWebService you can add to display a web service non-cached response plus the file version. We use file version instead of assembly version because we want to know a version, but with all assembly versions 1.0.0.0, the web site can be easily patched (signing and demand link still active!). Replace @Class@ with the name of the web api controller this service is embedded in. It's good for a go/nogo on a web service plus a quick version check.

  [Route("api/testWebService")]
  [AllowAnonymous]
  [HttpGet]
  public HttpResponseMessage TestWebService()
  {
      HttpResponseMessage responseMessage = Request.CreateResponse(HttpStatusCode.OK);
      string loc = Assembly.GetAssembly(typeof(@Class@)).Location;
      FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(loc);
      responseMessage.Content = new StringContent($"<h2>The XXXXX web service GET test succeeded.</h2>{DateTime.Now}<br/><br/>File Version: {versionInfo.FileVersion}");
      responseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
      Request.RegisterForDispose(responseMessage);
      return responseMessage;
  }

I found it also necessary to add the following to web.config under configuration to make it truly anonymous

<location path="api/testwebservice">
    <system.web>
        <authorization>
            <allow users="*" />
        </authorization>
    </system.web>
</location>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJL.View Question on Stackoverflow
Solution 1 - C#Ben AndersonView Answer on Stackoverflow
Solution 2 - C#KrisView Answer on Stackoverflow
Solution 3 - C#staaflView Answer on Stackoverflow
Solution 4 - C#ToteroView Answer on Stackoverflow
Solution 5 - C#Agent_9191View Answer on Stackoverflow
Solution 6 - C#MacSpudsterView Answer on Stackoverflow
Solution 7 - C#Prasan DuttView Answer on Stackoverflow
Solution 8 - C#InvincibleView Answer on Stackoverflow
Solution 9 - C#ArielView Answer on Stackoverflow
Solution 10 - C#Wray SmallwoodView Answer on Stackoverflow