Solved

.NET app.config in non-project directory - how to access?

Posted on 2007-04-10
4
2,504 Views
Last Modified: 2013-12-17
In a .NET C# project I want to place the app.config file in a different directory from the project directory, and be able to access it (ConfigurationManager methods).  How do I handle this case?

For example my app resides in:

c:\work\helloWorld\helloWorld.csproj

I want to put app.config under:

c:\mycomp_name\environment\apps\helloworld\app.config

How do I have my code recognize the app.config in the non-standard location/directory?
0
Comment
Question by:FredZimmerman
  • 2
4 Comments
 
LVL 18

Expert Comment

by:DarrenD
ID: 18883409
I'm not sure if this is possible.

The only thing I can think of is to create your own config file outside of the project and just read it in as a normal xml file and retrieve the values. Then any application can access the file to retrieve the values.

I'm not saying it's not possible.

Darren
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 18886121
You would have to have your app create another appdomain which then hosted your app ...

When creating the AppDomain you can specify the location of the App.Config (AppDomainSetup.ConfigurationFile http://msdn2.microsoft.com/en-us/system.appdomainsetup.configurationfile(VS.80).aspx http://www.c-sharpcorner.com/UploadFile/Ashish1/appdomain02132006085918AM/appdomain.aspx?ArticleID=9a7159d9-cac0-4246-bacd-7de46cb9d610 includes an example.

The reason it is done at the appdomain level like this is because the same file is used to control many appdomain options (like where to probe for dlls).


The other alternative which has already been mentioned above is to go through and create a custom app specific config file which is pretty easy to do using XML serialization.

Cheers,

Greg
0
 

Author Comment

by:FredZimmerman
ID: 18890654
I have question with regard to the ExeFilePath for the <app_name>.exe.config.

If I am pointing to an app.config outside my assembly/project directory, do I
set the directory to the Project/Assembly dir or the config file dir; see
following code:

// ASSEMBLY: c:\work\compname\projects\HelloWorld     - .csproj and .DLL reside here
// CONFIG: c:\compname\environment\HelloWorld\STAGING  - app.config goes here

// ? - What should exeFilePath point to?

            ExeConfigurationFileMap configFile = new ExeConfigurationFileMap();
            configFile.ExeConfigFilename = exeFilePath;
            Configuration config =
                ConfigurationManager.OpenMappedExeConfiguration(configFile,
                ConfigurationUserLevel.None);            
0
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 125 total points
ID: 18891892
its the ConfigurationUserLevel that specifies where the file is ... Check out http://msdn2.microsoft.com/en-us/library/system.configuration.configurationuserlevel.aspx

ConfigurationUserLevel.None specifies that it will be in the same directory as the executable.

This will also only apply to your own settings ... there are other settings (such as the CLR settings in the app.config which will not take effect if you use this method). As such you will have 2 config files.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question