tags:

views:

334

answers:

8

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?

+2  A: 

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

Try

Assembly.GetExecutingAssembly().Location 

or

Environment.CurrentDirectory
jgauffin
Thank you for the suggestion but it produces the same result.
mhenrixon
A: 

try this one:

  Module[] modules = Assembly.GetExecutingAssembly().GetModules();
  return Path.GetDirectoryName(modules[0].FullyQualifiedName);
Arseny
Thank you I did not try that one before. It still tries to find the xml file in the solution dir instead of the build dir though.
mhenrixon
you may registrate the assembly file in build directiry after copy them.
Arseny
A: 

Hi,

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

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

Or GetEntryAssembly()?

System.Reflection.Assembly.GetEntryAssembly().Location
andyp
+2  A: 

So it sounds/looks like your 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();
    }
csharptest.net
A: 

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.

GarethOwen
"If the loaded file was shadow-copied, the location is that of the file after being shadow-copied." / The Location property won't give the OP what he's after in this case.
hemp
A: 

Try

AppDomain.CurrentDomain.SetupInformation.PrivateBinPath
Conrad Frix
A: 

how about:

Application.StartupPath;
pieter.lowie
+1  A: 
string origAssemblyLocation = Assembly.GetExecutingAssembly().CodeBase;

Per MSDN:

Assembly.CodeBase Property

Gets the location of the assembly as specified originally

hemp
I had to remove the name of the dll but it does indeed get me the location on disk of the assembly!
mhenrixon