Version number of a dll in .NET

21,479

Solution 1

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.

Solution 2

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()...

Solution 3

 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).

Solution 4

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.

Share:
21,479

Related videos on Youtube

rein
Author by

rein

Updated on July 09, 2022

Comments

  • rein
    rein almost 2 years

    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.

  • Nick
    Nick over 14 years
    Be adviced: That return the Assembly version, not the File version. Both could differ.
  • Nick
    Nick over 14 years
    It is also not the same as the win32 function GetFileVersionInfo, which returns the File version and not the Assembly version.
  • rein
    rein over 14 years
    Thanks Lars, I somehow overlooked the FileVersionInfo class.
  • Steven Sudit
    Steven Sudit over 14 years
    Doesn't this also load the assembly, including executing static constructors?
  • EricSchaefer
    EricSchaefer over 14 years
    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?
  • shake
    shake over 13 years
    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.
  • Nick
    Nick over 10 years
    File Version and Product Version can also differ for managed dlls. [AssemblyFileVersion] vs [AssemblyInformationalVersion]. See stackoverflow.com/questions/7770068/… and stackoverflow.com/questions/64602/…
  • bdeem
    bdeem about 9 years
    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