Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make ConfigurationManager read a config file other than app.config?

I need to parse a config file that is situated in another project. I know that ConfigurationManager reads the app.config file by default, but how to make it read that particular config file?

like image 906
Amokrane Chentir Avatar asked May 10 '10 08:05

Amokrane Chentir


People also ask

How do I override ConfigurationManager appSettings?

It appears there is a way to do this in . NET 3.5 by setting the allowOverride attribute in the appSettings definition section of machine. config. This allows you to override the entire section in your own app.

What is the difference between web config and app config?

The web. config file is required for ASP.NET webpages. The app. config file is optional in an application and doesn't have to be used when writing desktop applications.

How do I open a config file in Visual Studio?

In Solution Explorer, right-click the project node, and then select Add > New Item. The Add New Item dialog box appears. Expand Installed > Visual C# Items. In the middle pane, select the Application Configuration File template.


2 Answers

// Create a filemap refering the config file.
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = configFilePath;

// Retrieve the config file.
Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
like image 86
Marcus Avatar answered Oct 02 '22 10:10

Marcus


Or like this:

var fileMap = new ConfigurationFileMap(configFilePath);
Configuration config = ConfigurationManager.OpenMappedMachineConfiguration(fileMap);

But you still will have a problem with custom configuration sections.

like image 21
Misha Dev Avatar answered Oct 02 '22 09:10

Misha Dev