public void RunWorkItem6053Demo() { Util.WriteResult("===== WorkItem6053 DEMO =====\r\n"); try { string newConfigFilePath = ""; Util.CreateWorkItem6053ConfigFile(out newConfigFilePath); Util.WriteResult("Created new config file! Path={0}\r\n", newConfigFilePath); Configuration config = Util.LoadConfigFile(newConfigFilePath); Util.WriteResult("Loaded config file.\r\n"); InnerConfigurationSection section = config.Sections["test1"] as InnerConfigurationSection; Util.WriteResult("\r\n### Configuration Read Results for InnerConfigurationSection:\r\n"); Util.WriteResult("\tTestAttribute1 = {0}\r\n", section.TestAttribute1); Util.WriteResult("\r\n### Configuration Read Results for ConfigurationSection1:\r\n"); ConfigurationSection1 section2 = config.Sections["test2"] as ConfigurationSection1; Util.WriteResult("\tTestAttribute2 = {0}\r\n", section2.TestAttribute2); /* * WriteResult("\tNumber of professors: {0}\r\n", section.Professors.Count); * WriteResult("\tNumber of students: {0}\r\n", section.Students.Count); * WriteResult("\tName of student '0': {0}\r\n", section.Students[0].Name); */ //Util.DeleteConfigFile(newConfigFilePath); } catch (Exception ex) { Util.WriteResult("Exception Occured! {0}\r\n\r\n", ex); } Util.WriteResult("Demo Complete!\r\n\r\n"); }
public static void CreateWorkItem6053ConfigFile(out string newConfigFilePath) { newConfigFilePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + ".config"); ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap { ExeConfigFilename = newConfigFilePath }; Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); // Create configuration //OuterSectionGroup outer = new OuterSectionGroup(); InnerConfigurationSection section = new InnerConfigurationSection(); config.Sections.Add("test1", section); section.TestAttribute1 = "mytest"; ConfigurationSection1 section1 = new ConfigurationSection1(); config.Sections.Add("test2", section1); section1.TestAttribute2 = "mytest2"; /* * Professor flimflop = new Professor { Name = "Dr. Flimflop", YearOfBirth = 1968 }; * section.Professors.Add(flimflop); * Professor mania = new Professor { Name = "Dr. Maniä", YearOfBirth = 1972 }; * section.Professors.Add(mania); * * Student johnson = new Student { Name = "Johnson", YearOfBirth = 1989 }; * section.Students.Add(johnson); * mania.Students.Add(johnson); */ config.Save(); }