确定已加载的装配体

时间:2020-03-05 18:41:45  来源:igfitidea点击:

如何确定.NET桌面应用程序已加载的所有程序集?我想将它们放在"关于"框中,以便我可以通过电话查询客户,以确定他们的PC上的XYZ版本。

很高兴看到托管和非托管程序集。我知道列表会很长,但是我打算对它进行增量搜索。

解决方案

回答

看起来像AppDomain.CurrentDomain.GetAssemblies();可以解决问题:)

回答

using System;
using System.Reflection;
using System.Windows.Forms;

public class MyAppDomain
{
  public static void Main(string[] args)
  {
    AppDomain ad = AppDomain.CurrentDomain;
    Assembly[] loadedAssemblies = ad.GetAssemblies();

    Console.WriteLine("Here are the assemblies loaded in this appdomain\n");
    foreach(Assembly a in loadedAssemblies)
    {
      Console.WriteLine(a.FullName);
    }
  }
}

回答

或者是System.Reflection.Assembly.GetLoadedModules()。

请注意,AppDomain.GetAssemblies将仅迭代当前AppDomain中的程序集。一个应用程序可能有多个AppDomain,因此可能会或者可能不会执行我们想要的操作。

回答

对于所有DLL(包括非托管DLL),我们都可以钉EnumProcessModules来获取模块句柄,然后对每个句柄使用GetModuleFileName来获取名称。

请参阅http://pinvoke.net/default.aspx/psapi.EnumProcessModules和http://msdn.microsoft.com/zh-cn/library/ms683197(VS.85).aspx(pinvoke.net没有签名)为此,但很容易弄清楚)。

对于64位,我们需要使用EnumProcessModulesEx