如何获取代码所在的程序集的路径?

假装没事ソ 提交于 2020-04-27 15:33:14

问题:

Is there a way to get the path for the assembly in which the current code resides? 有没有办法获取当前代码所在的程序集的路径? I do not want the path of the calling assembly, just the one containing the code. 我不希望调用程序集的路径,而只是包含代码的路径。

Basically my unit test needs to read some xml test files which are located relative to the dll. 基本上,我的单​​元测试需要读取一些相对于dll的xml测试文件。 I want the path to always resolve correctly regardless of whether the testing dll is run from TestDriven.NET, the MbUnit GUI or something else. 我希望该路径始终正确解析,而不管测试dll是从TestDriven.NET,MbUnit GUI还是其他版本运行。

Edit : People seem to be misunderstanding what I'm asking. 编辑 :人们似乎误解了我在问什么。

My test library is located in say 我的测试库位于

C:\\projects\\myapplication\\daotests\\bin\\Debug\\daotests.dll C:\\ projects \\ myapplication \\ daotests \\ bin \\ Debug \\ daotests.dll

and I would like to get this path: 我想走这条路:

C:\\projects\\myapplication\\daotests\\bin\\Debug\\ C:\\ projects \\ myapplication \\ daotests \\ bin \\ Debug \\

The three suggestions so far fail me when I run from the MbUnit Gui: 当我从MbUnit Gui运行时,到目前为止,这三个建议使我失望:

  • Environment.CurrentDirectory gives c:\\Program Files\\MbUnit Environment.CurrentDirectory给出c:\\ Program Files \\ MbUnit

  • System.Reflection.Assembly.GetAssembly(typeof(DaoTests)).Location gives C:\\Documents and Settings\\george\\Local Settings\\Temp\\ ....\\DaoTests.dll System.Reflection.Assembly.GetAssembly(typeof(DaoTests)).Location给出C:\\ Documents and Settings \\ george \\ Local Settings \\ Temp \\ .... \\ DaoTests.dll

  • System.Reflection.Assembly.GetExecutingAssembly().Location gives the same as the previous. System.Reflection.Assembly.GetExecutingAssembly().Location与上一个相同。


解决方案:

参考一: https://stackoom.com/question/DjZ/如何获取代码所在的程序集的路径
参考二: https://oldbug.net/q/DjZ/How-do-I-get-the-path-of-the-assembly-the-code-is-in
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!