有没有办法获取当前代码所在的程序集的路径?我不想要调用程序集的路径,只需要包含代码的路径.
基本上我的单元测试需要读取一些相对于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
与前一个相同.
我刚刚开始使用NUnit来为我的项目提供一些测试覆盖.
在我的主library.dll中,我需要从库,library.xml的外部文件中加载配置数据.
这在我使用库时工作正常,因为我使用以下内容来获取查找配置文件的目录:
string settingspath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
Run Code Online (Sandbox Code Playgroud)
我注意到的问题是,当我使用NUnit进行单元测试时,它会将我的程序集复制到Shadow Copy,但不会带任何其他文件,所以当然我的init由于缺少配置文件而失败.
我应该做些不同的事情来从我的库中找到配置文件吗?(这是一个服务器应用程序,我不想使用标准的应用程序设置,或用户的本地设置等)
我有几个图像,我想嵌入到exe中.
当我将构建操作设置为嵌入式资源时, 我解决了代码中出现资源不可用的错误,并要求我将构建操作设置为资源
我尝试了几种不同的方法:
<ImageSource x:Key="Image_Background">YearBook;component/Resources/Images/darkaurora.png</ImageSource>
<ImageSource x:Key="Image_Background">Images/darkaurora.png</ImageSource>
<ImageSource x:Key="Image_Background">pack://application:,,,/Resources/Images/darkaurora.png</ImageSource>
Run Code Online (Sandbox Code Playgroud)
此代码位于Resource文件中.但没有一个工作,他们都抛出这个错误:
Cannot convert the string 'pack://application:,,,/Resources/Images/darkaurora.png' into a 'System.Windows.Media.ImageSource' object. Cannot locate resource 'resources/images/darkaurora.png'. Error at object 'Image_Background' in markup file 'YearBook;component/Resources/ImageResources.xaml' Line 4 Position 6.
Run Code Online (Sandbox Code Playgroud)
在代码中的不同位置我得到:
the file 'YearBook;component/Resources/Images/shadowdrop.png' is not a part of the project or its 'Build Action' property is not set to 'Resource'
Run Code Online (Sandbox Code Playgroud)
那么,我做错了什么?
我喜欢这里的建议:
它建议使用此代码:
public class SettingsReader()
{
public SettingsReader(System.IO.StreamReader reader)
{
// read contents of stream...
}
}
// In production code:
new SettingsReader(new StreamReader(File.Open("settings.xml")));
// In unit test:
new SettingsReader(new StringReader("<settings>dummy settings</settings>"));
Run Code Online (Sandbox Code Playgroud)
我只是想知道最好的做法是"提供"大型测试字符串(即要解析的文件的几行).