How to Read settings from app.config in C#, How to save application settings to app.config in C#, How to Read settings from web.config in C#, How to save settings to web.config in C#

Read and Write config file in C#

  • How to Read settings from app.config in C#:

You can use the ConfigurationManager.AppSettings property to read settings from the app.config file in C#. Here is an example:

string settingValue = ConfigurationManager.AppSettings["MySetting"]; Console.WriteLine("MySetting value is: " + settingValue); 

This will read the value of the MySetting setting from the app.config file.

  • How to save application settings to app.config in C#:

You can use the Configuration class to save application settings to the app.config file in C#. Here is an example:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); config.AppSettings.Settings["MySetting"].Value = "NewValue"; config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("appSettings"); 

This will save the value "NewValue" to the MySetting setting in the app.config file.

  • How to Read settings from web.config in C#:

You can use the ConfigurationManager.AppSettings property to read settings from the web.config file in C#. Here is an example:

string settingValue = ConfigurationManager.AppSettings["MySetting"]; Console.WriteLine("MySetting value is: " + settingValue); 

This will read the value of the MySetting setting from the web.config file.

  • How to save settings to web.config in C#:

You can use the Configuration class to save settings to the web.config file in C#. Here is an example:

Configuration config = WebConfigurationManager.OpenWebConfiguration("~"); config.AppSettings.Settings["MySetting"].Value = "NewValue"; config.Save(); 

This will save the value "NewValue" to the MySetting setting in the web.config file. Note that you need to use WebConfigurationManager instead of ConfigurationManager to access the web.config file in a web application.

Examples

  1. C# Read and Write Config File Example:

    Read and write configuration settings in a C# application.

    // Read from config file string settingValue = ConfigurationManager.AppSettings["SettingKey"]; // Write to config file Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); configuration.AppSettings.Settings["SettingKey"].Value = "NewValue"; configuration.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("appSettings"); 
  2. ConfigurationManager in C#:

    Use the ConfigurationManager class to access configuration settings.

    string settingValue = ConfigurationManager.AppSettings["SettingKey"]; 
  3. Read app.config File in C#:

    Read settings from the app.config file.

    string settingValue = ConfigurationManager.AppSettings["SettingKey"]; 
  4. Write to app.config in C#:

    Write settings to the app.config file.

    Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); configuration.AppSettings.Settings["SettingKey"].Value = "NewValue"; configuration.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("appSettings"); 
  5. XML Configuration File in C#:

    Create and use an XML configuration file.

    // Reading XDocument doc = XDocument.Load("config.xml"); string settingValue = doc.Root.Element("SettingKey").Value; // Writing XDocument newDoc = new XDocument(new XElement("Settings", new XElement("SettingKey", "NewValue"))); newDoc.Save("config.xml"); 
  6. AppSettings in C#:

    Use the AppSettings section in the configuration file.

    string settingValue = ConfigurationManager.AppSettings["SettingKey"]; 
  7. Read and Write JSON Configuration File in C#:

    Read and write configuration settings in a JSON file.

    // Reading string json = File.ReadAllText("config.json"); var settings = JsonConvert.DeserializeObject<Dictionary<string, string>>(json); string settingValue = settings["SettingKey"]; // Writing settings["SettingKey"] = "NewValue"; string newJson = JsonConvert.SerializeObject(settings); File.WriteAllText("config.json", newJson); 
  8. INI File Handling in C#:

    Read and write configuration settings in an INI file.

    // Reading string settingValue = IniFile.ReadValue("config.ini", "Section", "SettingKey"); // Writing IniFile.WriteValue("config.ini", "Section", "SettingKey", "NewValue"); 
  9. Custom Configuration Sections in C#:

    Define and use custom configuration sections.

    MyConfigurationSection config = ConfigurationManager.GetSection("MyConfigurationSection") as MyConfigurationSection; string settingValue = config.SettingKey; 
  10. Reading Settings from Config File C#:

    Read various settings from a configuration file.

    // Reading different types of settings string stringValue = ConfigurationManager.AppSettings["StringKey"]; int intValue = int.Parse(ConfigurationManager.AppSettings["IntKey"]); bool boolValue = bool.Parse(ConfigurationManager.AppSettings["BoolKey"]); 
  11. ConfigurationManager.AppSettings C#:

    Access settings directly using ConfigurationManager.AppSettings.

    string settingValue = ConfigurationManager.AppSettings["SettingKey"]; 
  12. User-Specific Configuration in C#:

    Store user-specific configuration settings.

    // User-specific configuration ConfigurationUserLevel userLevel = ConfigurationUserLevel.PerUserRoaming; Configuration configuration = ConfigurationManager.OpenExeConfiguration(userLevel); string settingValue = configuration.AppSettings.Settings["SettingKey"].Value; 
  13. Encrypt and Secure Config Files in C#:

    Encrypt and secure sensitive information in the configuration file.

    // Encrypting sections Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); configuration.ConnectionStrings.SectionInformation.ProtectSection("DataProtectionConfigurationProvider"); configuration.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("connectionStrings"); 
  14. Working with ConfigurationManager.AppSettings in C#:

    Work with the ConfigurationManager.AppSettings collection.

    // Accessing and modifying settings string settingValue = ConfigurationManager.AppSettings["SettingKey"]; ConfigurationManager.AppSettings["SettingKey"] = "NewValue"; 

More Tags

time-series quantum-computing cloudfiles viewmodel react-boilerplate asp.net-web-api-routing background-service numeric monitor text-formatting

More Programming Guides

Other Guides

More Programming Examples