mhenrixon
mhenrixon

Reputation: 6278

Better way to get the base directory?

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?

Upvotes: 20

Views: 56951

Answers (9)

Abhishek
Abhishek

Reputation: 1

string baseDirectory=Application.StartupPath.Split(Path.DirectorySeparatorChar)[0];

Application startup path will return the path where exe is kept, we will split this using "\" and get the base directory as "C:" for example,

Upvotes: 0

csharptest.net
csharptest.net

Reputation: 64248

So it sounds/looks like you're attempting to obtain the configuration file for an assembly. The following should accomplish that task by accessing the 'Location' property of the assembly and using it to retrieve the configuration path:

static string GetConfigFileByType(Type type)
{
    Configuration config = 
        ConfigurationManager.OpenExeConfiguration(type.Assembly.Location);

    if (config.HasFile)
        return config.FilePath;
    throw new FileNotFoundException();
}

Upvotes: 9

Bamara Coulibaly
Bamara Coulibaly

Reputation: 737

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

Upvotes: 29

jgauffin
jgauffin

Reputation: 101176

Your question is a bit unclear. I don't really know if this is what you want.

I typically use AppDomain.CurrentDomain.BaseDirectory

Alternatives

  • Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
  • Environment.CurrentDirectory

Upvotes: 13

hemp
hemp

Reputation: 5683

string origAssemblyLocation = Assembly.GetExecutingAssembly().CodeBase;

Per MSDN:

Assembly.CodeBase Property

Gets the location of the assembly as specified originally

Upvotes: 11

pieter.lowie
pieter.lowie

Reputation: 108

how about:

Application.StartupPath;

Upvotes: 1

GarethOwen
GarethOwen

Reputation: 6133

I like to make my classes configurable - for example they get the folder name as a parameter in their constructor. This makes it possible to test with different config files.

In test code we use:

TestContext.TestDeploymentDir

This is the testrun folder, where all assemblies for a test run are copied into, together with the test deployment items. We 'deploy' our unit test config files to the test run folder - this can be specified in the testrunconfig dialog in visual studio.

For our production code we pass

Assembly.GetExecutingAssembly().Location

to the constructor, which works for us.

Upvotes: 1

andyp
andyp

Reputation: 6269

have you tried getting the FileName of the current process' MainModule?

System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName

Or GetEntryAssembly()?

System.Reflection.Assembly.GetEntryAssembly().Location

Upvotes: 1

Arseny
Arseny

Reputation: 7361

try this one:

  Module[] modules = Assembly.GetExecutingAssembly().GetModules();
  return Path.GetDirectoryName(modules[0].FullyQualifiedName);

Upvotes: 1

Related Questions