简体   繁体   English

.NET中dll的版本号

[英]Version number of a dll in .NET

Given the following: 鉴于以下内容:

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

How can I find the file and product version numbers of that DLL using .NET? 如何使用.NET查找该DLL的文件和产品版本号?

The dll can be either native or managed. dll可以是本机的或托管的。

Thanks. 谢谢。

Yes, using System.Diagnostics.FileVersionInfo . 是的,使用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. 程序集版本是程序集标识的一部分。

I don't know about native dlls but with managed dlls it works like this: 我不知道本机dll,但有托管dll它的工作原理如下:

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

EDIT: I think you can read the version info in C with GetFileVersionInfo() ... 编辑:我认为您可以使用GetFileVersionInfo()读取C中的版本信息...

 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). 在Windows中,“文件版本”和“产品版本”是相同的(或至少是托管的.dll)。

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 . 有关它们采用什么值,谁使用它们以及如何阅读它们的信息,请参阅http://all-things-pure.blogspot.com/2009/09/assembly-version-file-version-product.html

If you want the file version information then use the FileVersionInfo class ( FileVersionInfo documentation ) 如果您需要文件版本信息,请使用FileVersionInfo类( FileVersionInfo文档

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 有一个如何在代码plex上执行此操作的示例

for .net assembly file version may well be something 1.0. 对于.net程序集文件版本可能是1.0。 , 1.1. ,1.1。 1.2. 1.2。 , 2.0. ,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. 换句话说,发布者可能会选择修复项目的主要版本和次要版本,但将其余部分留给.net进行编译和增量。 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 上面的c#代码在执行.exe和执行.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. 当然,如果您尝试使用某人的dll,您可以使用Eclipsed4utoo或EricSchaefer的建议,具体取决于发布商如何选择分配版本和构建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM