Let's say I have the following scenario in .NET 4.0: - Solution containing: a) a Class Library b) a Console Application
The Console Application references the Class Library.
What I want is to setup some Application Settings in my Class Library and make it so that these are accessible by the Class Library (host Console Application should not make use of these directly), but also modifiable via a .config file after deployment (so if the user decides they want to change the value of one of the settings, they can do so without having to re-deploy the application.
Is this possible?
Figured this out after quite a bit of Google-fu.
The final solution I adopted was the following: I created Settings (which generated an app.config file) in my Service application. I then created links to these settings in both my Code Library project and Console Application project (this is done by going to Add > Existing Item > then hitting the drop-down arrow next to the Add button and selecting "Add as Link". Although the one in the Code Library project is not necessary.
What this does is make it so that I only have 1 settings configuration while in development, while it will still generate a config file for my console application which I can access from both the console application and the Code Library during the development process.
Finally, I used the code below to open the configuration file and access the values. As a disclaimer, there may be an easier way of doing this, but I tried about a 100 combinations and nothing else worked:
String cfgFileName = "IntercompanyConsoleApp.exe.config";
ExeConfigurationFileMap cfgMap = new ExeConfigurationFileMap();
cfgMap.ExeConfigFilename = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\" + cfgFileName;
Configuration cfg = ConfigurationManager.OpenMappedExeConfiguration(cfgMap, ConfigurationUserLevel.None);
// IntercompanyService is the name of my service app, which is where the real app.config file resides -- hence the entries in the xml are based on this application.
// Also, the scope of my settings entries is Application
ClientSettingsSection section = (ClientSettingsSection)cfg.GetSection("applicationSettings/IntercompanyService.Properties.Settings");
Console.WriteLine(section.Settings.Get("Server").Value.ValueXml.InnerText);
Console.WriteLine(section.Settings.Get("Database").Value.ValueXml.InnerText);
This is an obscure issue, but hopefully this saves someone some time in the future because I spent about 4 hours trying to figure this out.