// Get the application configuration file. // Note. This function uses the // OpenExeConfiguration(string)method // to get the application configuration file. public static void GetAppConfiguration() { // Get the application path needed to obtain // the application configuration file. string applicationName = Environment.GetCommandLineArgs()[0]; if (!applicationName.Contains(".exe")) { applicationName = String.Concat(applicationName, ".exe"); } string exePath = System.IO.Path.Combine( Environment.CurrentDirectory, applicationName); // Get the configuration file. The file name has // this format appname.exe.config. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(exePath); try { // Create a custom configuration section // having the same name used in the // roaming configuration file. // This is possible because the configuration // section can be overridden by other // configuration files. string sectionName = "consoleSection"; ConsoleSection customSection = new ConsoleSection(); if (config.Sections[sectionName] == null) { // Store console settings. customSection.ConsoleElement.BackgroundColor = ConsoleColor.Black; customSection.ConsoleElement.ForegroundColor = ConsoleColor.White; // Add configuration information to the // configuration file. config.Sections.Add(sectionName, customSection); config.Save(ConfigurationSaveMode.Modified); // Force a reload of the changed section. // This makes the new values available for reading. ConfigurationManager.RefreshSection(sectionName); } // Set console properties using values // stored in the configuration file. customSection = (ConsoleSection)config.GetSection(sectionName); Console.BackgroundColor = customSection.ConsoleElement.BackgroundColor; Console.ForegroundColor = customSection.ConsoleElement.ForegroundColor; // Apply the changes. Console.Clear(); } catch (ConfigurationErrorsException e) { Console.WriteLine("[Error exception: {0}]", e.ToString()); } // Display feedback. Console.WriteLine(); Console.WriteLine("Performed the following tasks:"); Console.WriteLine( "Created the application configuration file {0}", config.FilePath); Console.WriteLine("Created custom ConsoleSection."); Console.WriteLine( "Stored background (black) and foreground (white) colors."); Console.WriteLine( "Added the section to the configuration file."); Console.WriteLine( "Read stored colors from the configuration file."); Console.WriteLine("Applied the colors to the console window."); Console.WriteLine(); }
// Access a configuration file using mapping. // This function uses the OpenMappedExeConfiguration // method to access the roaming and the application // configuration files. // It then uses the the custom ConsoleSection from // each file to console window colors. public static void MapExeConfiguration() { // Cotains the selected configuration. System.Configuration.Configuration config; // Get the roaming configuration file. Configuration roamingConfig = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.PerUserRoaming); // Get the application configuration file. Configuration appConfig = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); // To map the selected configuration file. ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap(); // Map roaming configuration. This allows // access to the roaming configuration file. configFileMap.ExeConfigFilename = roamingConfig.FilePath; // Get the mapped configuration file config = ConfigurationManager.OpenMappedExeConfiguration( configFileMap, ConfigurationUserLevel.None); string sectionName = "consoleSection"; ConsoleSection customSection = (ConsoleSection)config.GetSection(sectionName); // Set console properties using the // configuration values contained in the // mapped configuration file. Console.BackgroundColor = customSection.ConsoleElement.BackgroundColor; Console.ForegroundColor = customSection.ConsoleElement.ForegroundColor; Console.Clear(); Console.WriteLine(); Console.WriteLine("Performed following tasks:"); Console.WriteLine( "Mapped roaming configuration file: {0}", config.FilePath); Console.WriteLine( "Set console colors from stored configuration values."); Console.WriteLine(); Console.Write( "Press Enter to switch to default colors....."); Console.ReadLine(); // Map roaming configuration. This allows // access to the application configuration file. configFileMap.ExeConfigFilename = appConfig.FilePath; // Get the mapped configuration file config = ConfigurationManager.OpenMappedExeConfiguration( configFileMap, ConfigurationUserLevel.None); customSection = (ConsoleSection)config.GetSection(sectionName); if (customSection == null) { customSection = new ConsoleSection(); // Store console settings. customSection.ConsoleElement.BackgroundColor = ConsoleColor.Black; customSection.ConsoleElement.ForegroundColor = ConsoleColor.White; // Add configuration information to the // configuration file. config.Sections.Add(sectionName, customSection); config.Save(ConfigurationSaveMode.Modified); // Force a reload of the changed section. // This makes the new values available for reading. ConfigurationManager.RefreshSection(sectionName); } // Set console properties using the // configuration values contained in the // mapped configuration file. Console.BackgroundColor = customSection.ConsoleElement.BackgroundColor; Console.ForegroundColor = customSection.ConsoleElement.ForegroundColor; Console.Clear(); Console.WriteLine( "Mapped application configuration file: {0}", config.FilePath); Console.WriteLine( "Set console colors from stored configuration values."); Console.WriteLine(); }
// Get the roaming configuration file associated // with the application. // Note.This function uses the OpenExeConfiguration( // ConfigurationUserLevel userLevel) method to // get the configuration file. public static void GetRoamingConfiguration() { // Define the custom section to add to the // configuration file. string sectionName = "consoleSection"; ConsoleSection cSection = null; // Get the roaming configuration // that applies to the current user. Configuration roamingConfig = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.PerUserRoaming); // Map the roaming configuration file. This // enables the application to access // the configuration file using the // System.Configuration.Configuration class ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap(); configFileMap.ExeConfigFilename = roamingConfig.FilePath; // Get the mapped configuration file. Configuration config = ConfigurationManager.OpenMappedExeConfiguration( configFileMap, ConfigurationUserLevel.None); try { cSection = (ConsoleSection)config.GetSection( sectionName); if (cSection == null) { // Create a custom configuration section. cSection = new ConsoleSection(); // Define where in the configuration file // hierarchy the associated // configuration section can be declared. cSection.SectionInformation.AllowExeDefinition = ConfigurationAllowExeDefinition.MachineToLocalUser; // Allow the configuration section to be // overridden by other configuration files. cSection.SectionInformation.AllowOverride = true; // Force the section to be saved. cSection.SectionInformation.ForceSave = true; // Store console settings for roaming users. cSection.ConsoleElement.BackgroundColor = ConsoleColor.Blue; cSection.ConsoleElement.ForegroundColor = ConsoleColor.Yellow; // Add configuration information to // the configuration file. config.Sections.Add(sectionName, cSection); config.Save(ConfigurationSaveMode.Modified); // Force a reload of the changed section. This // makes the new values available for reading. ConfigurationManager.RefreshSection( sectionName); } } catch (ConfigurationErrorsException e) { Console.WriteLine("[Exception error: {0}]", e.ToString()); } // Set console properties using values // stored in the configuration file. Console.BackgroundColor = cSection.ConsoleElement.BackgroundColor; Console.ForegroundColor = cSection.ConsoleElement.ForegroundColor; // Apply the changes. Console.Clear(); // Display feedback. Console.WriteLine(); Console.WriteLine("Performed the following tasks:"); Console.WriteLine( "Created roaming configuration file {0}", config.FilePath); Console.WriteLine("Created custom ConsoleSection."); Console.WriteLine( "Stored background (blue) and foreground (yellow) colors."); Console.WriteLine( "Added the section to the configuration file."); Console.WriteLine( "Read stored colors from the configuration file."); Console.WriteLine("Applied the colors to the console window."); Console.WriteLine(); }