Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better way to get the base directory?

Tags:

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?

like image 599
mhenrixon Avatar asked Jul 02 '10 05:07

mhenrixon


People also ask

What is base directory path?

The base directory is the path to a folder on your system that represents the application directory (%AppDir%) on the user's system.

What is base directory of your project?

The base directory is the path on your system that corresponds to the path where your application will be installed. In other words, it's the local representative of %AppDir%. (%AppDir% is the built-in variable that gets set to the installation path when the user selects an install folder at run time.)


1 Answers

differents ways to get the base directory

  1. AppDomain.CurrentDomain.BaseDirectory

  2. Directory.GetCurrentDirectory() // not guaranteed to work on Mobile application

  3. Environment.CurrentDirectory // this calls Directory.GetCurrentDirectory()

  4. this.GetType().Assembly.Location // Assembly.location

  5. Application.StartupPath // for windows forms apps

  6. Application.ExecutablePath // same as Application.StartupPath

like image 52
Bamara Coulibaly Avatar answered Sep 27 '22 21:09

Bamara Coulibaly