web analytics

ConfigurationManager.GetSection() vs Configuration.GetSection() in .NET Framework

Options

codeling 1602 - 6666
@2016-01-28 14:13:40

In .NET Framework, both methods provide you the abilities to access the configuration sections in your application configuration file. But there are some key differences between these two methods.

If your application needs read-only access to its own configuration, it is recommended you use the ConfigurationManager.GetSection() method. It provides access to the cached configuration values for the current application, which has better performance than the Configuration class.

If your application needs read and write access to its own configuration, you have to use Configuration.GetSection() methods, because the Configuration class allows programmatic access for editing configuration files.

The following C# code shows you how to make a copy of the configuration file named "source.config" using Configuration.GetSection() and Configuration.Save() methods:

//Load Source
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = "source.config";
Configuration source = ConfigurationManager.OpenMappedExeConfiguration(map,ConfigurationUserLevel.None);

//Load Target
map.ExeConfigFilename = "target.config";
Configuration target = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);

foreach (ConfigurationSection section in source.Sections)
{
   //We want to ensure that this guy came from the file we provided.. and not from say machine config
   if (section.ElementInformation.Source !=null && section.ElementInformation.Source.Equals(source.FilePath))
   {
      //Enumerator is on AppSettings. So we update the appSettings
      if (section is AppSettingsSection)
      {
         foreach (KeyValueConfigurationElement element in source.AppSettings.Settings)
         {
            target.AppSettings.Settings.Remove(element.Key);
            target.Save(ConfigurationSaveMode.Full, false);
            target.AppSettings.Settings.Add(element);
         }
       }
      //Enumerator is on a custom section
      else
      {
         //Remove from target and add from source. 
         target.Sections.Remove(section.SectionInformation.SectionName);
         //Just paranoid.
         target.Save(ConfigurationSaveMode.Full, false);
         //Using reflection to instantiate since no public ctor and the instance we hold is tied to "Source"
         ConfigurationSection reflectedSection = Activator.CreateInstance(section.GetType()) as ConfigurationSection;
         reflectedSection.SectionInformation.SetRawXml(section.SectionInformation.GetRawXml());
         //Bug/Feature in framework prevents target.Sections.Add(section.SectionInformation.Name, Section);
         target.Sections.Add(section.SectionInformation.Name, reflectedSection);
      }
         ConfigurationManager.RefreshSection(section.SectionInformation.SectionName);
   } 

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com