C# .NET 中 dll 的版本号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1537731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Version number of a dll in .NET
提问by rein
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.
谢谢。
采纳答案by Lars Truijens
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.
请注意,程序集的文件版本可能与其程序集版本不同。程序集版本是程序集标识的一部分。
回答by EricSchaefer
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()
...
编辑:我认为您可以使用 C 阅读版本信息GetFileVersionInfo()
...
回答by Justin
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
如果您需要程序集版本,则需要使用反射加载该程序集。有一个如何在代码丛上执行此操作的示例
回答by Ryan Alford
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 来说是相同的)。
回答by Ryan Alford
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。
回答by gg89
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
对于 .net 程序集文件版本很可能是 1.0。, 1.1. 1.2. , 2.0. 依此类推,换句话说,发布者可以选择修复项目的主要和次要版本,而将其余的留给 .net 来编译和增量。另一方面
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 的建议,具体取决于发布者如何选择分配版本和构建。