25

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.

rein
  • 32,967
  • 23
  • 82
  • 106

6 Answers6

38

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.

P Daddy
  • 28,912
  • 9
  • 68
  • 92
Lars Truijens
  • 42,837
  • 6
  • 126
  • 143
  • Thanks Lars, I somehow overlooked the FileVersionInfo class. – rein Oct 08 '09 at 13:31
  • 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 Sep 13 '10 at 13:02
  • Just be aware the FileVersionInfo.FileVersion is a string representation that may not be up to date. You should look at FileVersionInfo.FileMajorPart, FileMinorPart, FileBuildPart, FilePrivatePart. See [GetFileVersionInfo() returns wrong file's version information](http://stackoverflow.com/questions/5263954/getfileversioninfo-returns-wrong-files-version-information) – bdeem Feb 24 '15 at 16:28
4

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
  • 25,272
  • 21
  • 67
  • 103
  • 1
    Be adviced: That return the Assembly version, not the File version. Both could differ. – Lars Truijens Oct 08 '09 at 13:21
  • 1
    It is also not the same as the win32 function GetFileVersionInfo, which returns the File version and not the Assembly version. – Lars Truijens Oct 08 '09 at 13:22
  • 1
    Doesn't this also load the assembly, including executing static constructors? – Steven Sudit Oct 08 '09 at 13:57
  • 1
    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 Oct 08 '09 at 14:30
4
 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).

Ryan Alford
  • 7,514
  • 6
  • 42
  • 56
  • 1
    File Version and Product Version can also differ for managed dlls. [AssemblyFileVersion] vs [AssemblyInformationalVersion]. See http://stackoverflow.com/questions/7770068/get-the-net-assemblys-assemblyinformationalversion-value and http://stackoverflow.com/questions/64602/what-are-differences-between-assemblyversion-assemblyfileversion-and-assemblyin – Lars Truijens Feb 11 '14 at 14:13
1

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.

0

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

Justin
  • 84,773
  • 49
  • 224
  • 367
0

for .net assembly file version may well be something 1.0., 1.1. 1.2., 2.0. so on in other word, the publisher may be choose to fix the major and minor version for the project but leave the rest to .net to compile and increment. On the other hand

string version=System.Reflection.Assembly.GetExecutingAssembly()
    .GetName().Version.ToString();

will give the complete version number with major, minor, build numbers

the above c# code works in executing .exe and executing .dll

of course if you are trying use someone's dll you can use Eclipsed4utoo's or EricSchaefer's suggestion depending how publisher choose to assign the version and build.

gg89
  • 372
  • 3
  • 11