是否可以从任何.NET DLL以编程方式获取版本号?
如果有,怎么样?
如果dll是.net或Win32,则此方法有效.只有当dll是.net时,反射方法才有效.此外,如果使用反射,则需要将整个dll加载到内存中.以下方法不会将程序集加载到内存中.
// Get the file version for the notepad. FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(@"C:\MyAssembly.dll"); // Print the file name and version number. Console.WriteLine("File: " + myFileVersionInfo.FileDescription + '\n' + "Version number: " + myFileVersionInfo.FileVersion);
来自:http://msdn.microsoft.com/en-us/library/system.diagnostics.fileversioninfo.fileversion.aspx
原始来源
Assembly assembly = Assembly.LoadFrom("MyAssembly.dll"); Version ver = assembly.GetName().Version;
重要提示: 应该注意的是,这不是原始问题的最佳答案.不要忘记在此页面上阅读更多内容.
首先,您可能感兴趣的有两个可能的"版本":
Windows文件系统文件版本,适用于所有可执行文件
程序集构建版本,由编译器嵌入.NET程序集中(显然只适用于.NET程序集dll和exe文件)
在前一种情况下,你应该使用Ben Anderson的答案; 在后一种情况下,使用AssemblyName.GetAssemblyName(@"c:\path\to\file.dll").Version
或Tataro的答案,以防您的代码引用程序集.
请注意,您可以忽略使用.Load()
/ .LoadFrom()
methods的所有答案,因为这些答案实际上会在当前AppDomain中加载程序集 - 这类似于剪切树以查看它的年龄.
这是使用一些反射来获取包含特定类的DLL版本的好方法:
var ver = System.Reflection.Assembly.GetAssembly(typeof(!Class!)).GetName().Version;
只需更换!Class!与您希望获得的版本的DLL中定义的类的名称.
这是我首选的方法,因为如果我为不同的部署移动DLL,我不必更改文件路径.
为启动的程序集(winform,console app等)获取它...
using System.Reflection; ... Assembly.GetEntryAssembly().GetName().Version
Kris,你的版本在需要从实际的DLL文件中加载程序集时很有用(如果DLL在那里!),但是,如果DLL是EMBEDDED(即,不是文件而是嵌入式文件),则会出现非常多的错误DLL).
另一件事是,如果使用类似" 1.2012.0508.0101 "之类的版本控制方案,当获得版本字符串时,您实际上会得到" 1.2012.518.101 "; 注意缺少的零.
所以,这里有一些额外的函数来获取DLL的版本(嵌入式或DLL文件):
public static System.Reflection.Assembly GetAssembly(string pAssemblyName) { System.Reflection.Assembly tMyAssembly = null; if (string.IsNullOrEmpty(pAssemblyName)) { return tMyAssembly; } tMyAssembly = GetAssemblyEmbedded(pAssemblyName); if (tMyAssembly == null) { GetAssemblyDLL(pAssemblyName); } return tMyAssembly; }//System.Reflection.Assembly GetAssemblyEmbedded(string pAssemblyDisplayName) public static System.Reflection.Assembly GetAssemblyEmbedded(string pAssemblyDisplayName) { System.Reflection.Assembly tMyAssembly = null; if(string.IsNullOrEmpty(pAssemblyDisplayName)) { return tMyAssembly; } try //try #a { tMyAssembly = System.Reflection.Assembly.Load(pAssemblyDisplayName); }// try #a catch (Exception ex) { string m = ex.Message; }// try #a return tMyAssembly; }//System.Reflection.Assembly GetAssemblyEmbedded(string pAssemblyDisplayName) public static System.Reflection.Assembly GetAssemblyDLL(string pAssemblyNameDLL) { System.Reflection.Assembly tMyAssembly = null; if (string.IsNullOrEmpty(pAssemblyNameDLL)) { return tMyAssembly; } try //try #a { if (!pAssemblyNameDLL.ToLower().EndsWith(".dll")) { pAssemblyNameDLL += ".dll"; } tMyAssembly = System.Reflection.Assembly.LoadFrom(pAssemblyNameDLL); }// try #a catch (Exception ex) { string m = ex.Message; }// try #a return tMyAssembly; }//System.Reflection.Assembly GetAssemblyFile(string pAssemblyNameDLL) public static string GetVersionStringFromAssembly(string pAssemblyDisplayName) { string tVersion = "Unknown"; System.Reflection.Assembly tMyAssembly = null; tMyAssembly = GetAssembly(pAssemblyDisplayName); if (tMyAssembly == null) { return tVersion; } tVersion = GetVersionString(tMyAssembly.GetName().Version.ToString()); return tVersion; }//string GetVersionStringFromAssemblyEmbedded(string pAssemblyDisplayName) public static string GetVersionString(Version pVersion) { string tVersion = "Unknown"; if (pVersion == null) { return tVersion; } tVersion = GetVersionString(pVersion.ToString()); return tVersion; }//string GetVersionString(Version pVersion) public static string GetVersionString(string pVersionString) { string tVersion = "Unknown"; string[] aVersion; if (string.IsNullOrEmpty(pVersionString)) { return tVersion; } aVersion = pVersionString.Split('.'); if (aVersion.Length > 0) { tVersion = aVersion[0]; } if (aVersion.Length > 1) { tVersion += "." + aVersion[1]; } if (aVersion.Length > 2) { tVersion += "." + aVersion[2].PadLeft(4, '0'); } if (aVersion.Length > 3) { tVersion += "." + aVersion[3].PadLeft(4, '0'); } return tVersion; }//string GetVersionString(Version pVersion) public static string GetVersionStringFromAssemblyEmbedded(string pAssemblyDisplayName) { string tVersion = "Unknown"; System.Reflection.Assembly tMyAssembly = null; tMyAssembly = GetAssemblyEmbedded(pAssemblyDisplayName); if (tMyAssembly == null) { return tVersion; } tVersion = GetVersionString(tMyAssembly.GetName().Version.ToString()); return tVersion; }//string GetVersionStringFromAssemblyEmbedded(string pAssemblyDisplayName) public static string GetVersionStringFromAssemblyDLL(string pAssemblyDisplayName) { string tVersion = "Unknown"; System.Reflection.Assembly tMyAssembly = null; tMyAssembly = GetAssemblyDLL(pAssemblyDisplayName); if (tMyAssembly == null) { return tVersion; } tVersion = GetVersionString(tMyAssembly.GetName().Version.ToString()); return tVersion; }//string GetVersionStringFromAssemblyEmbedded(string pAssemblyDisplayName)