views:

173

answers:

2

Hi,

I'm parsing the App.config file of a project. This config file has been loaded from a caller project. Inside the called project, I have something like:

   XmlDocument xmlDoc = new XmlDocument();
   xmlDoc.Load("app.config");
   // Some parsing...

Unfortunately the app.config file is not correctly located. Apparently the Load method is browsing the ~/bin/Release directory of the caller project, but the app.config file is located in the ~ directory.

Is there any way I can load this App.config file correctly?

Thanks

+1  A: 

The correct way to load configuration information is to use the types in the System.Configuration namespace: don't pass it yourself (for many of the types you'll need to also reference System.Configuration.dll.

Visual Studio should copy a file called app.config to the correctly named file in the correct folder when you build: check the properties of the file are set to "Build Action: None, Copy to output directory: do not copy" (VS has a special rule, don't override it). This includes renaming the file to <assembly_name>.config, so if you assembly is myapp.exe, then the config file will correctly be myapp.exe.config.

Richard
I didn't use ConfigurationManager because I didn't find how to parse my app.config file with it. How can I access to methods like: SelectNodes or GetNamedItem etc..?
Amokrane
I've yet to find a good introductory (i.e. simple) example of custom sections and parsing them -- it is fairly easy once you know how. Must add to the blog to do list :-)
Richard
Thanks Richard!FYI I moved my second question here: http://stackoverflow.com/questions/2804390/how-to-parse-app-config-using-configurationmanager
Amokrane
+6  A: 

I don't understand why you need to do this. The configuration file App.Config is copied to the runtime directory at build time and renamed yourapplicationname.exe.config.

You can use the ConfigurationManager class to access the contents of that file.

klausbyskov
+1 yes - just use ConfigurationManager!!
marc_s