Version number of a dll in .NET
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.
Related videos on Youtube
rein
Updated on July 09, 2022Comments
-
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 over 14 yearsBe adviced: That return the Assembly version, not the File version. Both could differ.
-
Nick over 14 yearsIt is also not the same as the win32 function GetFileVersionInfo, which returns the File version and not the Assembly version.
-
rein over 14 yearsThanks Lars, I somehow overlooked the FileVersionInfo class.
-
Steven Sudit over 14 yearsDoesn't this also load the assembly, including executing static constructors?
-
EricSchaefer over 14 yearsIt 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 over 13 yearsI 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 over 10 yearsFile Version and Product Version can also differ for managed dlls. [AssemblyFileVersion] vs [AssemblyInformationalVersion]. See stackoverflow.com/questions/7770068/… and stackoverflow.com/questions/64602/…
-
bdeem about 9 yearsJust 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