如何获取当前正在执行的DLL的位置?

发布于 2024-10-13 04:16:03 字数 683 浏览 2 评论 0原文

我有一个配置文件,需要在执行我正在编写的 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")

I have a config file that I need to load as part of the execution of a dll I am writing.

The problem I am having is that the place I put the dll and config file is not the "current location" when the app is running.

For example, I put the dll and xml file here:

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

But if I try to reference the xml file (in my dll) like this:

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

then .\AggregatorItems.xml translates to:

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

So, I need to find a way (I hope) of knowing where the dll that is currently executing is located. Basically I am looking for this:

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

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

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

发布评论

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

评论(6

木槿暧夏七纪年 2024-10-20 04:16:04
System.Reflection.Assembly.GetExecutingAssembly().Location
System.Reflection.Assembly.GetExecutingAssembly().Location
风和你 2024-10-20 04:16:04

如果您正在使用 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 检查计算机上正在运行的进程。我写了详细的描述 此处

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

我已经写了关于如何对 IIS 内的 dll 执行此操作的完整描述 - http://nodogmablog.bryanhogan.net/2016/09/ located-and-checking-an-executing-dll-on-a-running-web-服务器/

If you're working with an asp.net application and you want to locate assemblies when using the debugger, they are usually put into some temp directory. I wrote the this method to help with that scenario.

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

For more details see this blog post http://nodogmablog.bryanhogan.net/2015/05/finding-the-location-of-a-running-assembly-in-net/

If you can't change the source code, or redeploy, but you can examine the running processes on the computer use Process Explorer. I written a detailed description here.

It will list all executing dlls on the system, you may need to determine the process id of your running application, but that is usually not too difficult.

I've written a full description of how do this for a dll inside IIS - http://nodogmablog.bryanhogan.net/2016/09/locating-and-checking-an-executing-dll-on-a-running-web-server/

笑咖 2024-10-20 04:16:04

试试这个

System.IO.Path.GetDirectoryName(new System.Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath)

Try this

System.IO.Path.GetDirectoryName(new System.Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath)

回忆躺在深渊里 2024-10-20 04:16:03

您正在寻找 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:\),您可能需要将其删除。

You are looking for System.Reflection.Assembly.GetExecutingAssembly()

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

Note:

The .Location property returns the location of the currently running DLL file.

Under some conditions the DLL is shadow copied before execution, and the .Location property will return the path of the copy. If you want the path of the original DLL, use the Assembly.GetExecutingAssembly().CodeBase property instead.

.CodeBase contains a prefix (file:\), which you may need to remove.

撩人痒 2024-10-20 04:16:03

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

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.

Reflection is your friend, as has been pointed out. But you need to use the correct method;

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.
浅听莫相离 2024-10-20 04:16:03

就我而言(处理将我的程序集作为文件加载到 Outlook 中):

typeof(OneOfMyTypes).Assembly.CodeBase

请注意在 Assembly 上使用 CodeBase (而不是 Location) 。其他人指出了定位组件的替代方法。

In my case (dealing with my assemblies loaded [as file] into Outlook):

typeof(OneOfMyTypes).Assembly.CodeBase

Note the use of CodeBase (not Location) on the Assembly. Others have pointed out alternative methods of locating the assembly.

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