// Create a custom section. // It will contain a nested section as // deined by the UrlsSection (<urls>...</urls>). static void CreateSection(string sectionName) { // Get the current configuration (file). System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); UrlsSection urlsSection; // Create an entry in the <configSections>. if (config.Sections [sectionName] == null) { urlsSection = new UrlsSection(); config.Sections.Add(sectionName, urlsSection); config.Save(); } // Create the actual target section and write it to // the configuration file. if (config.Sections ["/configuration/" + sectionName] == null) { urlsSection = config.GetSection(sectionName) as UrlsSection; urlsSection.SectionInformation.ForceSave = true; config.Save(ConfigurationSaveMode.Full); } }
static void ShowUrls() { try { UrlsSection myUrlsSection = ConfigurationManager.GetSection("MyUrls") as UrlsSection; if (myUrlsSection == null) { Console.WriteLine("Failed to load UrlsSection."); } else { Console.WriteLine("My URLs:"); PrintUrls(myUrlsSection.Urls); PrintUrls(myUrlsSection.Urls.GroupSettings); } } catch (Exception e) { Console.WriteLine(e.ToString()); } }
// Create a custom section. // It will contain a nested section as // deined by the UrlsSection (<urls>...</urls>). static void CreateSection (string sectionName) { // Get the current configuration (file). System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration ( ConfigurationUserLevel.None); UrlsSection urlsSection; // Create an entry in the <configSections>. if (config.Sections [sectionName] == null) { urlsSection = new UrlsSection (); config.Sections.Add (sectionName, urlsSection); config.Save (); } // Create the actual target section and write it to // the configuration file. if (config.Sections ["/configuration/" + sectionName] == null) { urlsSection = config.GetSection (sectionName) as UrlsSection; urlsSection.SectionInformation.ForceSave = true; config.Save (ConfigurationSaveMode.Full); } }