【问题标题】:How to get the location of the DLL currently executing?如何获取当前正在执行的 DLL 的位置?
【发布时间】:2011-06-13 11:24:36
【问题描述】:

我有一个配置文件,作为我正在编写的 dll 执行的一部分,我需要加载它。

我遇到的问题是,当应用程序运行时,我放置 dll 和配置文件的位置不是“当前位置”。

比如我把dll和xml文件放在这里:

D:\Program Files\Microsoft Team Foundation Server 2010\Application Tier\Web Services\bin\Plugins

但如果我尝试像这样引用 xml 文件(在我的 dll 中):

XDocument doc = XDocument.Load(@".\AggregatorItems.xml")

然后 .\AggregatorItems.xml 转换为:

C:\windows\system32\inetsrv\AggregatorItems.xml

所以,我需要找到一种方法(我希望)知道当前正在执行的 dll 的位置。基本上我正在寻找这个:

XDocument doc = XDocument.Load(CoolDLLClass.CurrentDirectory+@"\AggregatorItems.xml")

【问题讨论】:

    标签: c# .net dll file-io


    【解决方案1】:

    您正在寻找System.Reflection.Assembly.GetExecutingAssembly()

    string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
    string xmlFileName = Path.Combine(assemblyFolder,"AggregatorItems.xml");
    

    注意:

    .Location 属性返回当前运行的 DLL 文件的位置。

    在某些情况下,DLL 在执行前被影子复制,.Location 属性将返回复制的路径。如果您想要原始 DLL 的路径,请改用 Assembly.GetExecutingAssembly().CodeBase 属性。

    .CodeBase 包含一个前缀 (file:\),您可能需要将其删除。

    【讨论】:

    • 唉!返回C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\Temporary ASP.NET Files\\tfs\\de3c0c8e\\c1bdf790\\assembly\\dl3\\20b156cb\\22331f24_bfb9cb01\\AggregatorItems.xml
    • 啊!但是Assembly.GetExecutingAssembly().CodeBase 有!
    • CodeBase 给了我文件:\\c:\myassemblypath,这很奇怪
    • @Matt 使用 new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath 获取真实姓名
    • string curAssemblyFolder = new System.Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath;
    【解决方案2】:

    正如已经指出的那样,反思是你的朋友。但是你需要使用正确的方法;

    Assembly.GetEntryAssembly()     //gives you the entrypoint assembly for the process.
    Assembly.GetCallingAssembly()   // gives you the assembly from which the current method was called.
    Assembly.GetExecutingAssembly() // gives you the assembly in which the currently executing code is defined
    Assembly.GetAssembly( Type t )  // gives you the assembly in which the specified type is defined.
    

    【讨论】:

      【解决方案3】:

      在我的情况下(处理我的程序集 [作为文件] 加载到 Outlook 中):

      typeof(OneOfMyTypes).Assembly.CodeBase
      

      注意在Assembly 上使用CodeBase(不是Location)。其他人指出了定位程序集的替代方法。

      【讨论】:

        【解决方案4】:
        System.Reflection.Assembly.GetExecutingAssembly().Location
        

        【讨论】:

          【解决方案5】:

          如果您正在使用 asp.net 应用程序并且想要在使用调试器时定位程序集,通常会将它们放入某个临时目录中。我编写了这个方法来帮助解决这种情况。

          private string[] GetAssembly(string[] assemblyNames)
          {
              string [] locations = new string[assemblyNames.Length];
          
          
              for (int loop = 0; loop <= assemblyNames.Length - 1; loop++)       
              {
                   locations[loop] = AppDomain.CurrentDomain.GetAssemblies().Where(a => !a.IsDynamic && a.ManifestModule.Name == assemblyNames[loop]).Select(a => a.Location).FirstOrDefault();
              }
              return locations;
          }
          

          更多详情请看这篇博文http://nodogmablog.bryanhogan.net/2015/05/finding-the-location-of-a-running-assembly-in-net/

          如果您无法更改源代码或重新部署,但可以检查计算机上正在运行的进程,请使用 Process Explorer。我写了详细的说明here。

          它将列出系统上所有正在执行的 dll,您可能需要确定正在运行的应用程序的进程 ID,但这通常并不太难。

          我已经为 IIS 中的 dll 写了一个完整的描述 - http://nodogmablog.bryanhogan.net/2016/09/locating-and-checking-an-executing-dll-on-a-running-web-server/

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-01-10
            • 1970-01-01
            • 2016-03-06
            • 1970-01-01
            • 1970-01-01
            • 2010-09-07
            相关资源
            最近更新 更多