Version number of a dll in .NET

左心房为你撑大大i 提交于 2019-11-27 15:39:05

问题


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.


回答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.




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




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




回答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.




回答5:


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




回答6:


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.



来源:https://stackoverflow.com/questions/1537731/version-number-of-a-dll-in-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!