[英]Getting the Last Modified Date of an assembly in the GAC
我已经实现了许多帖子中提到的fusion.dll包装器,现在发现至少一个我需要确定是否需要更新的dll没有使用内部版本号和修订号。 因此,我无法比较版本号,而需要在上次修改日期进行比较。
fusion.dll或它的包装程序没有这样的方法,我认为这是足够公平的,但是如何确定dll的“真实”路径,以便可以发现它的上次修改日期。
到目前为止,我的代码:
private DateTime getGACVersionLastModified(string DLLName)
{
FileInfo fi = new FileInfo(DLLName);
string dllName = fi.Name.Replace(fi.Extension, "");
DateTime versionDT = new DateTime(1960,01,01);
IAssemblyEnum ae = AssemblyCache.CreateGACEnum();
IAssemblyName an;
AssemblyName name;
while (AssemblyCache.GetNextAssembly(ae, out an) == 0)
{
try
{
name = GetAssemblyName(an);
if (string.Compare(name.Name, dllName, true) == 0)
{
FileInfo dllfi = new FileInfo(string.Format("{0}.dll", name.Name));
if (DateTime.Compare(dllfi.LastWriteTime, versionDT) >= 0)
versionDT = dllfi.LastWriteTime;
}
}
catch (Exception ex)
{
logger.FatalException("Unable to get version number: ", ex);
}
}
return versionDT;
}
从问题的问题描述中,我可以看到您实际上要完成2个主要任务:
1)确定是否可以从GAC加载给定的程序集名称。
2)返回给定程序集的文件修改日期。
我相信可以轻松得多地完成这两点,而不必使用非托管融合API 。 执行此任务的更简单方法如下:
static void Main(string[] args)
{
// Run the method with a few test values
GetAssemblyDetail("System.Data"); // This should be in the GAC
GetAssemblyDetail("YourAssemblyName"); // This might be in the GAC
GetAssemblyDetail("ImaginaryAssembly"); // This just plain doesn't exist
}
private static DateTime? GetAssemblyDetail(string assemblyName)
{
Assembly a;
a = Assembly.LoadWithPartialName(assemblyName);
if (a != null)
{
Console.WriteLine("'{0}' is in GAC? {1}", assemblyName, a.GlobalAssemblyCache);
FileInfo fi = new FileInfo(a.Location);
Console.WriteLine("'{0}' Modified: {1}", assemblyName, fi.LastWriteTime);
return fi.LastWriteTime;
}
else
{
Console.WriteLine("Assembly '{0}' not found", assemblyName);
return null;
}
}
结果输出的示例:
GAC中是否存在“ System.Data”? 真正
修改了'System.Data':2010/10/1 9:32:27 AM
“ YourAssemblyName”在GAC中吗? 假
'YourAssemblyName'修改时间:12/30/2010 4:25:08 AM
找不到程序集“ ImaginaryAssembly”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.