using System.Configuration; namespace MyConfigSection { public class MySettings : ConfigurationSection { [ConfigurationProperty("isEnabled", DefaultValue = false, IsRequired = true)] public bool IsEnabled { get { return (bool)this["isEnabled"]; } set { this["isEnabled"] = value; } } [ConfigurationProperty("serverName", DefaultValue = "localhost", IsRequired = true)] public string ServerName { get { return (string)this["serverName"]; } set { this["serverName"] = value; } } // Add more properties here... } }
using System.Configuration; Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); MySettings mySettings = config.GetSection("mySettings") as MySettings; if (mySettings != null) { // Use the configuration settings... bool isEnabled = mySettings.IsEnabled; string serverName = mySettings.ServerName; // Access more properties here... }In this example, we open the application's configuration file and retrieve a `MySettings` configuration section. We can then use its properties to configure the application. These examples use the `System.Configuration` package library, which provides types to access configuration files for applications, as well as create custom configuration elements.