Professor Mustard
Professor Mustard

Reputation: 191

Determine referenced dll file version in C#

I have a C# solution, which references a dll I created from a different C# solution.

It is easy enough to determine my solution's product version with Application.ProductVersion. However, what I really need is a way to determine the file version of the exe and the dll separately, within my program. I want to display the dll and exe's file versions in my About dialog. What would the code look like to do this?

Upvotes: 14

Views: 18198

Answers (3)

dtb
dtb

Reputation: 217401

The simplest way is if you know a type in the referenced assembly:

AssemblyName name = typeof(MyCompany.MyLibrary.SomeType).Assembly.GetName();

Assembly.GetName returns an AssemblyName which has a Version property indicating the version of the assembly.

Alternatively, you can get the assembly names of all assemblies referenced by the executing assembly (i.e., the .exe):

AssemblyName[] names = Assembly.GetExecutingAssembly().GetReferencedAssemblies();

Upvotes: 27

womp
womp

Reputation: 116987

The AssemblyInfo class has all this information, so you just need to get a reference to the assembly in your code. For example:

Assembly.GetExecutingAssembly.GetName.Version.ToString()

You can get the other assemblies in the project in various ways, for example

var assemblies = Assembly.GetExecutingAssembly().GetReferencedAssemblies();

Upvotes: 3

Tamas Czinege
Tamas Czinege

Reputation: 121424

Perhaps the easiest solution is this:

var version = Assembly.GetAssembly(typeof(SomeType)).GetName().Version;

where SomeType is a type you know for sure that is defined in that particular assembly. You can then call .ToString() on this version object or look at its properties.

Of course, this will blow up in a huge fireball the moment you move your type into another assembly. If this is a possibility, you will need a more robust way to find the assembly object. Let me know if this is the case.

Upvotes: 4

Related Questions