获取 GAC 中程序集的上次修改日期

发布于 2024-10-09 09:17:45 字数 1027 浏览 3 评论 0原文

我已经实现了许多帖子中提到的 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;
}

I have implemented the fusion.dll wrapper mentioned in many posts and now find that at least one dll I need to determine if it needs to be updated is not using build and revision numbers. Consequently I cannot compare on version numbers and need to compare on Last Modified date.

fusion.dll or it's wrappers have no such method which I guess is fair enough but how do I determine the 'real' path of the dll so that I can discovers it's last Modified date.

My code so far:

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

や三分注定 2024-10-16 09:17:45

从您问题中的问题描述中,我可以看到您确实想要完成 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;
  }
}

结果输出的示例:

“System.Data”在 GAC 中吗?真实
“系统数据”已修改:2010 年 10 月 1 日上午 9:32:27
“YourAssemblyName”在 GAC 中吗?错误
“您的程序集名称”已修改:2010 年 12 月 30 日上午 4:25:08
找不到程序集“ImaginaryAssembly”

From the problem description in your question I can see there are really 2 primary tasks that you are trying to accomplish:

1) Determine if a given assembly name can be loaded from the GAC.
2) Return the file modified date for the given assembly.

I believe these 2 points can be accomplished in a much simpler fashion and without having to work with the unmanaged fusion API. An easier way to go about this task might be as follows:

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;
  }
}

An example of the resulting output:

'System.Data' is in GAC? True
'System.Data' Modified: 10/1/2010 9:32:27 AM
'YourAssemblyName' is in GAC? False
'YourAssemblyName' Modified: 12/30/2010 4:25:08 AM
Assembly 'ImaginaryAssembly' not found

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文