Amokrane Chentir
Amokrane Chentir

Reputation: 30405

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?

Upvotes: 14

Views: 6835

Answers (2)

Misha  Dev
Misha Dev

Reputation: 119

Or like this:

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

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

Upvotes: 1

Marcus
Marcus

Reputation: 1876

// 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);

Upvotes: 23

Related Questions