Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine referenced dll file version in C#

Tags:

c#

.net

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?

like image 700
Professor Mustard Avatar asked Jan 25 '10 20:01

Professor Mustard


People also ask

How do I tell what version of dll is installed?

If you reference the dll in Visual Studio right click it (in ProjectName/References folder) and select "Properties" you have "Version" and "Runtime Version" there. In File Explorer when you right click the dll file and select properties there is a "File Version" and "Product Version" there.

How do I find the PowerShell assembly version?

You can get file version of file using PowerShell Get-Command cmdlet. In the above command, Get-Command get dll assembly file version of the specified file using FileVersionInfo. FileVersion property.


2 Answers

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();
like image 98
dtb Avatar answered Sep 27 '22 17:09

dtb


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.

like image 32
Tamas Czinege Avatar answered Sep 27 '22 18:09

Tamas Czinege