public void CreateConfigFile(SampleRunnerOptions options, out string newConfigFilePath) { newConfigFilePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + ".config"); ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap { ExeConfigFilename = newConfigFilePath }; var config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None, options.EnablePreloadConfigFileCheckBox); // Create configuration SchoolRegistrySection section = new SchoolRegistrySection(); // NOTE: Can really be any name, but the same name must be used when reading. config.Sections.Add("schoolRegistrySection", section); 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(); }
private void CreateSchoolRegistryConfigFile(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 SchoolRegistrySection section = new SchoolRegistrySection(); config.Sections.Add("school", section); 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(); }
private void RunSchoolRegistryConfigDemo(string configFilePath) { bool isTempConfigFile = string.IsNullOrEmpty(configFilePath); WriteStartResult("SCHOOLS DEMO"); try { string newConfigFilePath = ""; // Create temp config file if no config specified. if (isTempConfigFile) { CreateSchoolRegistryConfigFile(out newConfigFilePath); WriteResult("Created new config file! Path={0}\r\n", newConfigFilePath); configFilePath = newConfigFilePath; } Configuration config = LoadConfigFile(configFilePath, isTempConfigFile); WriteResult("Loaded config file. Results:\r\n"); SchoolRegistrySection section = config.Sections["school"] as SchoolRegistrySection; WriteResult("\tSchool name: {0}\r\n", section.SchoolName); 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); // Delete config file if temp. if (isTempConfigFile) { DeleteConfigFile(configFilePath); } } catch (Exception ex) { WriteResult("\r\nException Occured! {0}\r\n", ex); } finally { WriteEndResult(); } }