One of my friends that I was working with had a brief question regarding using a uniform application configuration file between projects. In aggregate, he was wondering if it was feasible to use the orthodox ConfigurationManager.OpenExeConfiguration in order to pass values between applications, in this case, between a WinForm application that would write the values, and a Windows Service that would subsequently retrieve / manipulate these values. Indeed, it is possible, but not really in a standard fashion. If you are solely interested in a method to do so, scroll down to the bottom of this post as the method is listed there.
So, let’s look at some code and start to make sense of this whole thing, and then look at the final solution.
Firstly, we normally can return a value from the AppSettings section of an application configuration file using two main objects, namely the Configuration object, as well as the AppSettingsSection object. This takes on the form of:
private void ReturnAndSetXMLValue()
Configuration l_oConfiguration = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
AppSettingsSection l_oConfigSection = (AppSettingsSection)l_oConfiguration.GetSection(“appSettings”);
string l_strXmlValue = l_oConfigSection.Settings[“someXMLValue”].Value;
When we are performing a fairly simple operation, such as in this case setting the text property of some random .NET control to the value retrieve from the XML configuration, so we would normally place this in the loading handler or something. If you were saving the value instead, you could use (I am negating using the AppSettingsSection object here and just calling it literally, this is just for shits and giggles):
l_oConfiguration.AppSettings.Settings[“someXmlValue”].Value = adamBuenz;
But how would one call the application configuration file from another application? The solution is actually quite simple. I have written out a small method that will allow you to pass the XML value that you wish to harvest into the method as a parameter, returning a string that represents the configuration value so that you can do whatever with it. In order to use the below method, you only need to change the executable path, which represents the path to the configuration file that you wish to consume within the method, and then decide on what XML value you wish to pull in by setting the parameter.
private readonly string m_strExecutableConfigPath = “C:\\Program Files\\example”;
private string ReturnConfigurationValue(string strXmlParam)
ExeConfigurationFileMap l_oConfigFile = new ExeConfigurationFileMap();
configFile.ExeConfigFilename = m_strExecutableConfigPath;
Configuration l_oConfig = ConfigurationManager.OpenMappedExeConfiguration(l_oConfigFile, ConfigurationUserLevel.None);
// Throw some exception here because the config file doesn’t exist
AppSettingsSection l_oSection = (AppSettingsSection)config.GetSection(“appSettings”);
string l_strReturnedXML = l_oSection.Settings[strXmlParam].Value;
That should return your value. Pretty simple, eh?