是否可以从任何.NET DLL以编程方式获取版本号?
如果有,怎么样?
Assembly assembly = Assembly.LoadFrom("MyAssembly.dll");
Version ver = assembly.GetName().Version;
重要提示:应该注意的是,这不是原始问题的最佳答案。不要忘记在此页面上阅读更多内容。
虽然原始问题可能不是特定于Web服务,但这里有一个完整的testWebService,您可以添加它以显示Web服务非缓存响应和文件版本。我们使用文件版本而不是汇编版本,因为我们想知道一个版本,但是对于所有汇编版本1.0.0.0,可以轻松修补网站(签名和需求链接仍然有效!)。将@ Class @替换为此服务所嵌入的web api控制器的名称。这对于web服务上的go / nogo和快速版本检查是有好处的。
[Route("api/testWebService")]
[AllowAnonymous]
[HttpGet]
public HttpResponseMessage TestWebService()
{
HttpResponseMessage responseMessage = Request.CreateResponse(HttpStatusCode.OK);
string loc = Assembly.GetAssembly(typeof(@Class@)).Location;
FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(loc);
responseMessage.Content = new StringContent($"<h2>The XXXXX web service GET test succeeded.</h2>{DateTime.Now}<br/><br/>File Version: {versionInfo.FileVersion}");
responseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
Request.RegisterForDispose(responseMessage);
return responseMessage;
}
我发现还需要将以下内容添加到配置下的web.config中,以使其真正匿名
<location path="api/testwebservice">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
如果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
首先,您可能感兴趣的有两个可能的“版本”:
在前一种情况下,你应该使用Ben Anderson的答案;在后一种情况下,如果您的代码引用了程序集,请使用AssemblyName.GetAssemblyName(@"c:\path\to\file.dll").Version
或Tataro的答案。
请注意,您可以忽略使用.Load()
/ .LoadFrom()
方法的所有答案,因为这些方法实际上会在当前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)
var versionAttrib = new AssemblyName(Assembly.GetExecutingAssembly().FullName);
您可以使用System.Reflection.Assembly.Load *()方法,然后获取其AssemblyInfo。
@Ben回答证明对我有用。但我需要检查产品版本,因为它是我的软件中发生的主要增量,并遵循语义版本。
myFileVersionInfo.ProductVersion
这种方法符合我的期望
更新:我们可以使用Assembly获取产品版本,而不是在程序中明确提及dll路径(根据生产版本的需要)。
Assembly assembly = Assembly.GetExecutingAssembly();
FileVersionInfo fileVersionInfo =FileVersionInfo.GetVersionInfo(assembly.Location);
string ProdVersion= fileVersionInfo.ProductVersion;