Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取GAC中程序集的上次修改日期_C#_Reflection_Assemblies_Gac - Fatal编程技术网

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

C# 获取GAC中程序集的上次修改日期,c#,reflection,assemblies,gac,C#,Reflection,Assemblies,Gac,我已经实现了许多帖子中提到的fusion.dll包装器,现在发现至少有一个dll没有使用内部版本号和修订号,我需要确定它是否需要更新。因此,我无法比较版本号,需要比较上次修改日期 fusion.dll或它的包装器没有这样的方法,我想这是公平的,但我如何确定dll的“真实”路径,以便发现它的最后修改日期 到目前为止,我的代码是: private DateTime getGACVersionLastModified(string DLLName) { FileInfo fi = new File

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

从您问题中的问题描述中,我可以看出您正试图完成两项主要任务:

1) 确定是否可以从GAC加载给定的程序集名称。
2) 返回给定程序集的文件修改日期

我相信这两点可以用一种简单得多的方式来完成,而不必使用。执行此任务的更简单方法可能如下所示:

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年1月10日上午9:32:27
“YourAssemblyName”在GAC中?假
“YourAssemblyName”修改日期:2010年12月30日上午4:25:08
未找到程序集“ImaginaryAssembly”


从您问题中的问题描述中,我可以看出您正试图完成两项主要任务:

1) 确定是否可以从GAC加载给定的程序集名称。
2) 返回给定程序集的文件修改日期

我相信这两点可以用一种简单得多的方式来完成,而不必使用。执行此任务的更简单方法可能如下所示:

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年1月10日上午9:32:27
“YourAssemblyName”在GAC中?假
“YourAssemblyName”修改日期:2010年12月30日上午4:25:08
未找到程序集“ImaginaryAssembly”


GAC COM API并非没有文档记录。请参阅Mattias,感谢您指出fusion API的文档。我没有意识到这一点,并更新了我的答案摘要,以反映它实际上是有文档记录的。David,我想尝试比较多个版本是一个更复杂的问题。如果您修改上面的代码,请使用Assembly.Load(AssemblyName)而不是LoadWithPartialName,并使用程序集的强名称,您可能能够比较版本(我还没有测试过)。这假设您知道多个版本的强名称。否则,LoadWithPartialName将根据MSDN加载“最高版本号”。。。在您的情况下,考虑到您的程序集没有使用版本号和版本号,我不知道这会起什么作用。GAC COM API不是未记录的。请参阅Mattias,感谢您指出fusion API的文档。我没有意识到这一点,并更新了我的答案摘要,以反映它实际上是有文档记录的。David,我想尝试比较多个版本是一个更复杂的问题。如果您修改上面的代码,请使用Assembly.Load(AssemblyName)而不是LoadWithPartialName,并使用程序集的强名称,您可能能够比较版本(我还没有测试过)。这假设您知道多个版本的强名称。否则,LoadWithPartialName将根据MSDN加载“最高版本号”。。。在您的情况下,考虑到您的程序集不使用版本号和版本号,我不知道这会做什么。我已经很久没有使用Fusion API了。但是你试过调用iasemblycache.queryasemblyinfo吗?除其他外,它应该返回一个文件路径。但是你试过调用iasemblycache.queryasemblyinfo吗?除其他外,它应该返回一个文件路径。