我正在为我正在编写的简单单元测试而烦恼!
[TestFixture]
Class A
{
[TestMethod]
public void test()
{
Assembly.GetExecutingAssembly().Location // gives some temporary local path
}
}
我想要的只是获取程序集位置的路径(我的项目文件夹所在的位置)而不是某些本地或临时文件夹。
如果我输入断点,我会得到这个文件夹:\ AppData \ Local \ Temp \
非常感谢任何帮助!
编辑:我试过用nUnit和MSTest microsoft的单元测试来做这个!什么都没有帮助。答案 0 :(得分:1)
下面的代码将起作用。
Path.GetDirectoryName(new System.Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath)
答案 1 :(得分:0)
你试过吗
AppDomain.CurrentDomain.BaseDirectory
答案 2 :(得分:0)
对于ASP.Net,它不起作用。我在Why AppDomain.CurrentDomain.BaseDirectory not contains "bin" in asp.net app?找到了一个更好的解决方案。它适用于Win应用程序和ASP.Net Web应用程序。
public string ApplicationPath
{
get
{
if (String.IsNullOrEmpty(AppDomain.CurrentDomain.RelativeSearchPath))
{
return AppDomain.CurrentDomain.BaseDirectory; //exe folder for WinForms, Consoles, Windows Services
}
else
{
return AppDomain.CurrentDomain.RelativeSearchPath; //bin folder for Web Apps
}
}
}
答案 3 :(得分:0)
如果GetExecutingAssembly()
不是您所期望的可执行文件,为什么不在所需的程序集中使用已知类型。
typeof(A).Assembly.Location;