I am struggling with the configuration and setting classes in .NET 2.0
If the following is contaned in a file called app.config
<config>
<appSettings>
<add key="Foo" value="Hello World!"/>
</appSettings>
</config>
I know I can access the appSetting by
// this returns "Hello World!"
ConfigurationManager.AppSettings["Foo"]
However if the file is called app1.config
(or any other name) I cannot access the appSetting.
As long as I understand, with ConfigurationManager.OpenExeConfiguration
I should read custom config setting files.
Configuration conf = ConfigurationManager.OpenExeConfiguration(@"..\..\app1.config");
// this prints an empty string.
Console.WriteLine(conf.AppSettings.Settings["Foo"]);
However conf.AppSettings.Settings["Foo"]
returns an empty string.
I have also tried the following code but no success
ExeConfigurationFileMap exeFileMap = new ExeConfigurationFileMap();
exeFileMap.ExeConfigFilename = System.IO.Directory.GetCurrentDirectory()
+ "\\App1.config";
Configuration myConf = ConfigurationManager.OpenMappedExeConfiguration
(exeFileMap, ConfigurationUserLevel.None);
// returns empty string as well
Console.WriteLine(myConf.AppSettings.Settings["Foo"]);
How to read setting from a file not called app.config?
You should make use of a Settings-File, it's way more comfortable to use, has save and load methods and you can name it what ever you want. Eg. my Settings-File is called "EditorSettings.settings" and I access its properties like this:
I have created custom file myCustomConfiguration and changes its property Copy to Output Directory to true
In CS file
Although this is working but I am also looking for better approach.