以编程方式获取DLL的版本号

是否有可能从任何.NET DLL编程获得版本号?

如果是的话,怎么样?

Assembly assembly = Assembly.LoadFrom("MyAssembly.dll"); Version ver = assembly.GetName().Version; 

重要提示:应该指出,这不是对原始问题的最佳答案。 不要忘记阅读更多的内容。

如果dll是.netWin32,这将起作用。 如果dll是.net,则reflection方法才起作用。 此外,如果你使用reflection,你有负载整个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

原始来源

首先,您可能会感兴趣的有两种可能的“版本”:

  • Windows文件系统文件版本,适用于所有可执行文件

  • 程序集版本,由编译器embedded到.NET程序集中(显然只适用于.NET程序集dll和exe文件)

在前一种情况下,你应该使用本安德森的答案; 在后一种情况下,使用AssemblyName.GetAssemblyName(@"c:\path\to\file.dll").Version或Tataro的答案,以防程序集引用程序集。

请注意,您可以忽略使用.Load() / .LoadFrom()方法的所有答案,因为这些方法实际上将程序集加载到当前的AppDomain中 – 这类似于裁减一棵树以查看它的年龄。

为了得到它启动的程序集(winform,控制台应用程序等)

 using System.Reflection; ... Assembly.GetEntryAssembly().GetName().Version 

这里有一个很好的方法,使用一些reflection来获取包含特定类的DLL的版本:

 var ver = System.Reflection.Assembly.GetAssembly(typeof(!Class!)).GetName().Version; 

只要更换! 用您希望获取版本的DLL中定义的类的名称。

这是我首选的方法,因为如果我将DLL移到不同的部署中,我不必更改文件path。

克里斯,你的版本需要从实际的DLL文件(如果DLL是存在的!)加载程序集,但是,如果DLL是EMBEDDED将得到一个非常不必要的错误(即,不是一个文件,但embeddedDLL)。

另一方面,如果使用“ 1.2012.0508.0101 ”之类的版本scheme,当获得版本string时,实际上会得到“ 1.2012.518.101 ”; 注意缺失的零

所以,这里有一些额外的函数来获取DLL的版本(embedded式或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) 

您可以使用System.Reflection.Assembly.Load *()方法,然后获取其AssemblyInfo。

 var versionAttrib = new AssemblyName(Assembly.GetExecutingAssembly().FullName);