Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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

23 Experts available now in Live!

Get 1:1 Help Now