相关疑难解决方法(0)

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

有没有办法获取当前代码所在的程序集的路径?我不想要调用程序集的路径,只需要包含代码的路径.

基本上我的单元测试需要读取一些相对于dll的xml测试文件.无论测试dll是从TestDriven.NET,MbUnit GUI还是其他东西运行,我都希望路径始终正确解析.

编辑:人们似乎误解了我的要求.

我的测试库位于说

C:\项目\ MyApplication的\ daotests\BIN \调试\ daotests.dll

我想得到这条道路:

C:\项目\ MyApplication的\ daotests\BIN \调试\

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

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

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

  • System.Reflection.Assembly.GetExecutingAssembly().Location 与前一个相同.

.net c# reflection

746
推荐指数
20
解决办法
58万
查看次数

在单元测试输出目录中添加内容文件

我是visual studio中的单元测试的新手,我想加载一个物理xml文件.此文件作为内容在单元测试项目中,并在输出目录中复制.

因此,当我编译项目时,xml文件位于输出目录中.但是当我执行测试时,会创建一个包含所有相关DLL的新目录,但不会复制xml文件.

执行测试需要Xml的内容.我执行此代码以检索执行文件夹中的Xml文件的路径:

private static string GetXmlFullName()
{
    // GetApplicationPath use the current DLL to find the physical path
    // of the current application
    string executeDirectory = AssemblyHelper.GetApplicationPath();
    return Path.Combine(executeDirectory, "content.xml");
}
Run Code Online (Sandbox Code Playgroud)

例外是:

System.IO.DirectoryNotFoundException: 'd:\***\solutiondirectory\testresults\*** 2012-06-13 17_59_53\out\content.xml'.
Run Code Online (Sandbox Code Playgroud)

如何在execute文件夹中添加此文件?

提前致谢.(对不起我的英文...)

c# unit-testing visual-studio

8
推荐指数
1
解决办法
6950
查看次数

标签 统计

c# ×2

.net ×1

reflection ×1

unit-testing ×1

visual-studio ×1