//</Snippet9> //<Snippet10> static void AddSection() { try { System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); CustomSection customSection = new CustomSection(); string index = config.Sections.Count.ToString(); customSection.FileName = "newFile" + index + ".txt"; string sectionName = "CustomSection" + index; TimeSpan ts = new TimeSpan(0, 15, 0); customSection.MaxIdleTime = ts; customSection.MaxUsers = 100; config.Sections.Add(sectionName, customSection); customSection.SectionInformation.ForceSave = true; config.Save(ConfigurationSaveMode.Full); } catch (ConfigurationErrorsException err) { Console.WriteLine(err.ToString()); } }
//</Snippet3> //<Snippet4> static void GetSection() { try { CustomSection customSection = ConfigurationManager.GetSection( "CustomSection") as CustomSection; if (customSection == null) { Console.WriteLine( "Failed to load CustomSection."); } else { // Display section information Console.WriteLine("Defaults:"); Console.WriteLine("File Name: {0}", customSection.FileName); Console.WriteLine("Max Users: {0}", customSection.MaxUsers); Console.WriteLine("Max Idle Time: {0}", customSection.MaxIdleTime); } } catch (ConfigurationErrorsException err) { Console.WriteLine(err.ToString()); } }
//</Snippet8> //<Snippet9> static void Remove() { try { System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); CustomSection customSection = config.GetSection( "CustomSection") as CustomSection; if (customSection != null) { config.Sections.Remove("CustomSection"); customSection.SectionInformation.ForceSave = true; config.Save(ConfigurationSaveMode.Full); } else { Console.WriteLine( "CustomSection does not exists."); } } catch (ConfigurationErrorsException err) { Console.WriteLine(err.ToString()); } }
// Create a custom section. static void CreateSection() { try { CustomSection customSection; // Get the current configuration file. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); // Create the section entry // in the <configSections> and the // related target section in <configuration>. if (config.Sections["CustomSection"] == null) { customSection = new CustomSection(); config.Sections.Add("CustomSection", customSection); customSection.SectionInformation.ForceSave = true; config.Save(ConfigurationSaveMode.Full); } } catch (ConfigurationErrorsException err) { Console.WriteLine(err.ToString()); } }