更好的方式来获得基本目录? [英] Better way to get the base directory?

查看:96
本文介绍了更好的方式来获得基本目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码加载配置文件和读取所有的价值观和运行应用程序时,它工作正常,但当然对团队市失败,因为应用程序域的基本目录是构建脚本(psake)开始。我知道在执行测试之前,我可以改变目录到build目录,但我认为这是更好地使装载在任何时候配置文件的工作不管。

  XDocument.Load(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,CFGFILE)); 

有另一种方式来获得BaseDirectory中真正起作用的时刻?我试过下面也有同样的结果:

 路径字符串= Path.GetDirectoryName(Assembly.GetExecutingAssembly()基本代码); 
XDocument.Load(Path.Combine(路径,CFGFILE));

修改1 问题如下。我的解决方案,基本目录为C:\Project,所有编译文件拷贝到C:\Project\build。现在在psake构建脚本我有以下代码:

 任务测试-depends PrepareTests {
尝试{
$#=老PWD
$ #CD build_dir
和$ mspec $ mspec_projects --teamcity
$ #CD旧的
}
抓{
和;回声错误开始测试运行
出口1;
}
}



正如你可以看到我注释掉目录的变化这使得BaseDirectory构建文件/解决方案,而不是建立目录的位置,无论我如何努力对其进行访问。混淆样,如果你问我。



更新我真的想知道是否有可能获得组件的目录不管是什么目录启动该应用程序域所在的应用程序。如何


解决方案

 字符串origAssemblyLocation = Assembly.GetExecutingAssembly()基本代码?。 



每MSDN:




Assembly.CodeBase地产



获取装配为
的位置指定最初



I have this code to load a config file and read all the values and it works fine when running the application but of course fails on team city because the appdomain's base directory is where the build script (psake) is started. I know I can change directory to the build dir before executing the tests but I thought it's better to make loading the config file work at all times regardless.

XDocument.Load(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, cfgFile));

Is there another way to get the "BaseDirectory" that really works all times? I tried the below as well with same results:

string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
XDocument.Load(Path.Combine(path, cfgFile));

EDIT 1 The problem is the following. My solutions base directory is "C:\Project", all compiled files are copied to "C:\Project\build". Now in the psake build script I have the following code:

task Test -depends PrepareTests {
    try { 
        #$old = pwd
        #cd $build_dir
        &$mspec $mspec_projects --teamcity
        #cd $old
    }
    catch {
        &echo "Error starting test runner"
        Exit 1;
    }      
}

As you can see I commented out the changing of directories which makes the BaseDirectory the location of the build file / solution instead of the build directory regardless of how I try to access it. Kind of confusing if you ask me.

UPDATE I really like to know if it is possible to get the directory of the assembly regardless of what directory the application that started the app domain is located. How?

解决方案

string origAssemblyLocation = Assembly.GetExecutingAssembly().CodeBase;

Per MSDN:

Assembly.CodeBase Property

Gets the location of the assembly as specified originally

这篇关于更好的方式来获得基本目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆