如何获得当前正在执行的DLL的位置? [英] How to get the location of the DLL currently executing?

查看:226
本文介绍了如何获得当前正在执行的DLL的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我需要加载的DLL中我写的执行部分的配置文件。

我遇到的问题是,这个地方我把DLL和配置文件是不是当前位置应用程序运行时。

例如,我把该dll和xml文件的位置:

  

D:\ Program Files文件\微软的Team Foundation Server 2010 \应用层\ Web服务\ BIN \插件

但是,如果我尝试引用的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)
 

解决方案

您正在寻找<一href="http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getexecutingassembly.aspx"><$c$c>GetExecutingAssembly()

 字符串assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly()位置。);
串xmlFileName = Path.Combine(assemblyFolderAggregatorItems.xml);
 

编辑:

显然,位置属性不正确地在某些情况下工作(使用NUnit测试,TFS实例化DLL,展望?) - 在这种情况下,你可以使用 codeBase的属性。

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")

解决方案

You are looking for GetExecutingAssembly()

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

Edit:

Apparently the Location property does not work correctly under some conditions (testing using NUnit, TFS instantiated DLL, Outlook?) - in that case you can use the CodeBase property.

这篇关于如何获得当前正在执行的DLL的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆