Solved

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

Posted on 2007-04-10
4
2,503 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Data is not showing from images 15 37
Error on Add method 1 38
Entity Framework 7 32
Show/Hide ASP.NET controls with JQuery best practices 3 19
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now