如何获取当前正在执行的DLL的位置?
我有一个配置文件,需要在执行我正在编写的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您正在使用 ASP.NET 应用程序并且希望在使用调试器时找到程序集,它们通常会被放入某个临时目录中。我编写了这个方法来帮助解决这种情况。
有关更多详细信息,请参阅此博客文章 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.
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/
试试这个
Try this
您正在寻找
System.Reflection.Assembly。 GetExecutingAssembly()
注意:
.Location
属性返回当前运行的 DLL 文件的位置。在某些情况下,DLL 在执行前会进行卷影复制,
.Location
属性将返回副本的路径。如果您需要原始 DLL 的路径,请改用Assembly.GetExecutingAssembly().CodeBase
属性。.CodeBase
包含前缀 (file:\
),您可能需要将其删除。You are looking for
System.Reflection.Assembly.GetExecutingAssembly()
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 theAssembly.GetExecutingAssembly().CodeBase
property instead..CodeBase
contains a prefix (file:\
), which you may need to remove.正如已经指出的那样,反思是你的朋友。但你需要使用正确的方法;
Reflection is your friend, as has been pointed out. But you need to use the correct method;
就我而言(处理将我的程序集作为文件加载到 Outlook 中):
请注意在
Assembly
上使用CodeBase
(而不是Location
) 。其他人指出了定位组件的替代方法。In my case (dealing with my assemblies loaded [as file] into Outlook):
Note the use of
CodeBase
(notLocation
) on theAssembly
. Others have pointed out alternative methods of locating the assembly.