I know I can open config files that are related to an assembly with the static ConfigurationManager.OpenExe(exePath)
method but I just want to open a config that is not related to an assembly. Just a standard .NET config file.
Asked
Active
Viewed 1.2e+01k times
130
3 Answers
267
the articles posted by Ricky are very good, but unfortunately they don't answer your question.
To solve your problem you should try this piece of code:
ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
configMap.ExeConfigFilename = @"d:\test\justAConfigFile.config.whateverYouLikeExtension";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
If need to access a value within the config you can use the index operator:
config.AppSettings.Settings["test"].Value;

Oliver
- 43,366
- 8
- 94
- 151
-
1@Oliver By `whateverYouLikeExtension`, do you mean that you *must* have *something* after `config.`? – Svish Jan 16 '15 at 13:27
-
5@Oliver Got around to try now, and seems to work fine without :) – Svish Jan 16 '15 at 17:38
-
@Oliver how can I write to AppSettings of this config. It raises "Cannot access protected internal indexer" now. – optim1st Jun 08 '15 at 10:14
-
1I did this, but when I access ConfigurationManager.ConnectionStrings I'm still gettting the old data. What am I missing? – MAW74656 Dec 07 '15 at 20:25
-
1@MAW74656: You don't have to access `ConfigurationManager.ConnectionStrings`. Instead you have to read the value from the `config` object returned from the last statement above. – Oliver Dec 08 '15 at 07:42
-
So I can't "pull the rug out", have everything in my app just use the new config file? – MAW74656 Dec 09 '15 at 17:59
-
@MAW74656: I don't understand what you exactly mean. Maybe ask a new question and elaborate explain what you like to achieve. – Oliver Dec 10 '15 at 08:41
-
@Oliver Can I write system.diagnostics for WCF tracing in "justAConfigFile.config.whateverYouLikeExtension"? – FaizanHussainRabbani Aug 01 '16 at 16:58
-
I am always getting null while reading sections from my custom config. – Shesha Sep 21 '16 at 04:33
-
@Shesha: Then start a new question, describe what you do and what you expect. I'm sure, you'll get an answer. – Oliver Sep 21 '16 at 06:37
-
1thanks, this works! Except Configuration.AppSettings.Settings is not the same collection type as ConfigurationManager.AppSettings, but it is a minor issue. – serop Dec 21 '16 at 20:14
-
3For anyone else searching on how to get the appSettings after this is done: var foo = config.AppSettings.Settings["test"].Value; – Roro May 26 '17 at 17:53
-
Its worth adding, if you use "ExeConfigFilename " it doesn't fail if it cannot load the config - instead it just shows some default sections with no config. It would have been nice to get an exception.... ` ExeConfigurationFileMap configMap = new ExeConfigurationFileMap(); configMap.ExeConfigFilename = @".\junk.xml"; Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);` However, if you set configMap.MachineConfigFilename = @".\junk.xml"; it does fail. Use MachineConfigFilename... – James Joyce May 27 '20 at 05:26
8
The config file is just an XML file, you can open it by:
private static XmlDocument loadConfigDocument()
{
XmlDocument doc = null;
try
{
doc = new XmlDocument();
doc.Load(getConfigFilePath());
return doc;
}
catch (System.IO.FileNotFoundException e)
{
throw new Exception("No configuration file found.", e);
}
catch (Exception ex)
{
return null;
}
}
and later retrieving values by:
// retrieve appSettings node
XmlNode node = doc.SelectSingleNode("//appSettings");

Otávio Décio
- 73,752
- 17
- 161
- 228
-
Unreachable code detected after `throw new Exception("No configuration file found.", e);`. – Oybek Nov 09 '12 at 10:30
-
-
8why to use XML when you have such great classes from .Net library. I wouldn't suggest using this, poor on design. What next? implement a different string class... consider this. – Yuki Apr 08 '14 at 15:41
-
@OtávioDécio Can I add system.diagnostics to enable tracing in custom .config file? – FaizanHussainRabbani Aug 01 '16 at 09:12
-
@FaizanRabbani not sure about the custom tracing, but according to https://msdn.microsoft.com/en-us/library/ms733025(v=vs.110).aspx you should be able to add diagnostics on the config file. – Otávio Décio Aug 01 '16 at 13:24
-
@OtávioDécio My WCF service communicates with 4 other WCF services using different custom config file for each, now I want to enable tracing only on one WCF, not all of them. – FaizanHussainRabbani Aug 02 '16 at 09:04