Solved

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

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

809 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