kamagra how much to take

Working With Application Config Files Remotely

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:

  1. private void ReturnAndSetXMLValue()
  2. {
  3. Configuration l_oConfiguration = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
  4. AppSettingsSection l_oConfigSection = (AppSettingsSection)l_oConfiguration.GetSection("appSettings");
  5. string l_strXmlValue = l_oConfigSection.Settings["someXMLValue"].Value;
  6. <someControl>.Text = l_strXmlValue;
  7. }

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):

  1. l_oConfiguration.AppSettings.Settings["someXmlValue"].Value = adamBuenz;
  2. l_oConfiguration.Save(ConfigurationSaveMode.Modified);
  3. ConfigurationManager.RefreshSection("appSettings");

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.

  1. private readonly string m_strExecutableConfigPath = "C:\\Program Files\\example";
  2.  
  3. private string ReturnConfigurationValue(string strXmlParam)
  4. {
  5. ExeConfigurationFileMap l_oConfigFile = new ExeConfigurationFileMap();
  6. configFile.ExeConfigFilename = m_strExecutableConfigPath;
  7. Configuration l_oConfig =  ConfigurationManager.OpenMappedExeConfiguration(l_oConfigFile, ConfigurationUserLevel.None);
  8.  
  9. if (!File.Exists(l_oConfigFile.ExeConfigFilename))
  10. {
  11. // Throw some exception here because the config file doesn't exist
  12. }
  13.  
  14. AppSettingsSection l_oSection = (AppSettingsSection)config.GetSection("appSettings");
  15. string l_strReturnedXML = l_oSection.Settings[strXmlParam].Value;
  16. return l_strReturnedXML;
  17. }

That should return your value. Pretty simple, eh?

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>