Showing posts with label config. Show all posts
Showing posts with label config. Show all posts

May 2, 2008

Initialize a configuration section

in the configuration file, not config section is defined, you can use the following code to initialize the configuration.

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
MyConfigSection sec = fred.GetSection("MyConfigSection") as MyConfigSection;
if (sec == null)
{
  Console.WriteLine("sec is null");
  sec = new MyConfigSection();
  sec.SectionInformation.AllowDefinition = ConfigurationAllowDefinition.MachineToApplication;
  sec.Age = 12;
  config.Sections.Add("mysec", sec);
  config.Save(ConfigurationSaveMode.Minimal);
}

Please note that the sec should be also initialized, if there is not, only the Section definiton is defined, but not the actual configuration section.

RefreshSection not fresh

ConfigurationManager.RefershSection does not really refresh the section. To refersh, you need to OpenConfiuration and GetSection again. RefreshSection does not have any effect at all.

May 1, 2008

Use KeyValueConfigurationCollection

AppSettings mapped to

            
            
            
            
Then we write our code the access that seciton. Please note that we are not going to use the ConfigurationSection itself, what we are interested is the property in ConfigurationSetting, which is AppSettingsSection.Settings . Below is the code.
            public class Settings
            {
            private static KeyValueConfigurationCollection keywords;
            private static System.Configuration.Configuration config;
            private static System.Configuration.Configuration Config
            {
            get
            {
            if (config == null)
            {
            config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            }
            return config;
            }
            }

            public static KeyValueConfigurationCollection Keywords
            {
            get
            {
            if (keywords == null)
            {
            AppSettingsSection section = Config.GetSection("Keywords") as AppSettingsSection;
            keywords = section.Settings;
            }
            return keywords;
            }
            }

            public static void Save()
            {
            Config.Save();
            }
            }

            

To use the the settings, we can write the following code.

            Console.WriteLine(Settings.Keywords.Count.ToString());
            Settings.Keywords.Add("type", "html");
            Settings.Save();
            

ConfigurationSection

If you want to have your custom configuration, the first step is to create a ConfigurationSection. ConfigurationSection itself it is also a ConfigurationElement. Sometime your configuration setting can be very simple, for example it only has some properties of build in type such as string or boolean. Sometime it has complex property in this case you need to create a use ConfigurationElement or ConfigurationElementCollection to support that property. The same thing applies to the new ConfigurationElement.

Apr 30, 2008

debuging configuration .net

The .net configuration save feature can not be used with debugging , and the configSource can only work with configurationSection, it does not work with configurationGroup.

Jun 28, 2007

encrypt configuration file

aspnet_regiis -pe "appSettings" -app "/gbsa" -prov "DataProtectionConfigurationProvider"

aspnet_regiis -pe "appSettings" -app "/gbsa" -prov "Rsa ProtectedConfigurationProvider"