views:

362

answers:

5

Given the following:

string file = @"c:\somepath\somefile.dll";

How can I find the file and product version numbers of that DLL using .NET?

The dll can be either native or managed.

Thanks.

+2  A: 

I don't know about native dlls but with managed dlls it works like this:

System.Reflection.Assembly.LoadFile(file).GetName().Version

EDIT: I think you can read the version info in C with GetFileVersionInfo()...

EricSchaefer
Be adviced: That return the Assembly version, not the File version. Both could differ.
Lars Truijens
It is also not the same as the win32 function GetFileVersionInfo, which returns the File version and not the Assembly version.
Lars Truijens
You are right, ofcourse.
EricSchaefer
Doesn't this also load the assembly, including executing static constructors?
Steven Sudit
It does. But I believe the static ctors are not called before their classes are used. E.g. your static ctors are not called on app startup, but when you first use a static method of that classes or instantiate objects. But I may recall that wrong. Jon?
EricSchaefer
+14  A: 

Yes, using System.Diagnostics.FileVersionInfo.

string fileVersion = FileVersionInfo.GetVersionInfo(file).FileVersion;
string productVersion = FileVersionInfo.GetVersionInfo(file).ProductVersion;

Be advised that the file version of an assembly could be different from its assembly version. The assembly version is part of the assembly's identity.

Lars Truijens
Thanks Lars, I somehow overlooked the FileVersionInfo class.
rein
I have a problem with this FileVersionInfo class. I can not find it. I tried using namespace System::Diagnostics but it is not in diagnostics. I tried clr project, empty c++ project, i linked version.lib, system.dll and still can not find it. Please help.
shake
A: 

If you want the file version information then use the FileVersionInfo class (FileVersionInfo documentation)

If you want the assembly version then you will need to load that assembly using reflection. There is an example of how to do this on code plex

Kragen
+3  A: 
 FileVersionInfo fi = FileVersionInfo.GetVersionInfo(path);
 string fileVersion = fi.FileVersion;

In Windows, the "File Version" and "Product Version" are the same(or atleast it is for a managed .dll).

Eclipsed4utoo
A: 

There are three version numbers in an assembly. For info on what values they take, who uses them, and how to read them, see http://all-things-pure.blogspot.com/2009/09/assembly-version-file-version-product.html.

Charlie