public bool loadAPIConfiguration(ref com.show.api.APISection customSection) { string configFile = System.IO.Path.Combine( Environment.CurrentDirectory, configFileName); if (!File.Exists(configFile)) { return(false); } // Map the new configuration file. ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap(); configFileMap.ExeConfigFilename = configFile; // Get the configuration file. The file name has // this format appname.exe.config. // Get the mapped configuration file System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration( configFileMap, ConfigurationUserLevel.None); try { // Create a custom configuration section // having the same name that is used in the // roaming configuration file. // This is because the configuration section // can be overridden by lower-level // configuration files. // See the GetRoamingConfiguration() function in // this example. string sectionName = "apiSection"; if (config.Sections[sectionName] == null) { return(false); } // Set console properties using values // stored in the configuration file. customSection = (com.show.api.APISection)config.GetSection(sectionName); } catch (ConfigurationErrorsException e) { Console.WriteLine("[Error exception: {0}]", e.ToString()); } // Display feedback. Console.WriteLine(); Console.WriteLine("Using OpenExeConfiguration(string)."); // Display the current configuration file path. Console.WriteLine("Configuration file is: {0}", config.FilePath); return(true); }
public bool storeAPIConfiguration(com.show.api.APISection customSection) { string configFile = System.IO.Path.Combine( Environment.CurrentDirectory, configFileName); // Map the new configuration file. ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap(); configFileMap.ExeConfigFilename = configFile; // Get the configuration file. The file name has // this format appname.exe.config. // Get the mapped configuration file System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration( configFileMap, ConfigurationUserLevel.None); try { // Create a custom configuration section // having the same name that is used in the // roaming configuration file. // This is because the configuration section // can be overridden by lower-level // configuration files. // See the GetRoamingConfiguration() function in // this example. string sectionName = "apiSection"; if (config.Sections[sectionName] == null) { // Create a custom section if it does // not exist yet. // Store console settings. // 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); return(true); } else { // Set console properties using values // stored in the configuration file. com.show.api.APISection tmpSection = (com.show.api.APISection)config.GetSection(sectionName); if (tmpSection.ApiElement.AppID.Equals(customSection.ApiElement.AppID) && tmpSection.ApiElement.Secret.Equals(customSection.ApiElement.Secret)) { return(true); } else { tmpSection.ApiElement.AppID = customSection.ApiElement.AppID; tmpSection.ApiElement.Secret = customSection.ApiElement.Secret; customSection.SectionInformation.ForceSave = true; config.Save(ConfigurationSaveMode.Full); return(true); } } } catch (ConfigurationErrorsException e) { Console.WriteLine("[Error exception: {0}]", e.ToString()); } // Display feedback. Console.WriteLine("[Error] storeAPIConfiguration"); Console.WriteLine("Using OpenExeConfiguration(string)."); // Display the current configuration file path. Console.WriteLine("Configuration file is: {0}", config.FilePath); return(false); }