是否有方法获取当前代码所在程序集的路径?我不需要调用程序集的路径,只需要包含代码的路径。

基本上,我的单元测试需要读取一些相对于dll的xml测试文件。无论测试dll是否从TestDriven运行,我都希望该路径始终能够正确解析。NET, MbUnit GUI或者别的什么。

编辑:人们似乎误解了我的问题。

我的测试库位于say

C: \ \项目myapplication \ daotests \ bin \ \ daotests.dll调试

我想得到这条路径:

C: \ \ myapplication \ daotests \ bin \项目调试\

到目前为止,当我从MbUnit Gui运行时,这三个建议都失败了:

环境。CurrentDirectory 给出c:\Program Files\MbUnit System.Reflection.Assembly.GetAssembly .Location (typeof (DaoTests)) 给出C:\Documents和 乔治\ \本地设置 设置\ Temp \…\ DaoTests.dll .Location System.Reflection.Assembly.GetExecutingAssembly () 给出与前面相同的结果。


当前回答

我用这个来获取Bin目录的路径:

var i = Environment.CurrentDirectory.LastIndexOf(@"\");
var path = Environment.CurrentDirectory.Substring(0,i); 

你会得到这个结果:

“c: \ \ ricooley \文档\ visual studio用户 2010 \ \ Windows_Test_Project \ Windows_Test_Project \ bin项目”

其他回答

这应该可以工作,除非程序集是影子复制的:

string path = System.Reflection.Assembly.GetExecutingAssembly().Location
string path = Path.GetDirectoryName(typeof(DaoTests).Module.FullyQualifiedName);

我发现我的解足够检索位置。

var executingAssembly = new FileInfo((Assembly.GetExecutingAssembly().Location)).Directory.FullName;

您可以通过获取bin路径 AppDomain.CurrentDomain.RelativeSearchPath

我用这个来获取Bin目录的路径:

var i = Environment.CurrentDirectory.LastIndexOf(@"\");
var path = Environment.CurrentDirectory.Substring(0,i); 

你会得到这个结果:

“c: \ \ ricooley \文档\ visual studio用户 2010 \ \ Windows_Test_Project \ Windows_Test_Project \ bin项目”