Configuration Class
Assembly: System.Configuration (in system.configuration.dll)
The Configuration class instance represents the merged view of the configuration settings that apply to a specific physical entity, such as a computer, or to a logical entity, such as an application, or a Web site. The specified logical entity can exist on the local computer or on a remote server.
When no configuration file exists for a specified entity, the Configuration object represents the default configuration settings as defined by the Machine.config file.
You can get a Configuration object using one of the open configuration methods as defined by the following classes:
-
The ConfigurationManager class, if your entity is a client application.
-
The WebConfigurationManager class, if your entity is a Web application.
To generate a configuration file representing the inherited configuration settings for a specified entity, use one of the save-configuration methods:
-
The Save method to create a new configuration file.
-
The SaveAs method to generate a new configuration file at another location.
Note: |
|---|
| To enable access to configuration settings on a remote computer, use the Aspnet_regiis command-line tool. For more information about this tool, see ASP.NET IIS Registration Tool (Aspnet_regiis.exe). For information about creating and accessing custom configuration settings other than the intrinsic sections included in the .NET Framework, refer to ConfigurationSection. |
-
Read permission on the configuration file at the current configuration hierarchy level.
-
Read permissions on all the parent configuration files.
Note: |
|---|
| If you use a static GetSection method that takes a path parameter, the path parameter must refer to the application in which the code is running, otherwise the parameter is ignored and configuration information for the currently-running application is returned. |
-
Write permission on the configuration file and directory at the current configuration hierarchy level.
-
Read permissions on all the configuration files.
| Topic | Location |
|---|---|
| How to: Connect to an Oracle Database Using the SqlDataSource Control | Building ASP .NET Web Applications |
| How to: Connect to an Oracle Database Using the SqlDataSource Control | Building ASP .NET Web Applications |
The following code example demonstrates how to use the Configuration class to create a configuration file containing a custom section.
// Create a custom section.
static void CreateSection()
{
try
{
CustomSection customSection;
// Get the current configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None);
// Create the section entry
// in <configSections> and the
// related target section in <configuration>.
if (config.Sections["CustomSection"] == null)
{
customSection = new CustomSection();
config.Sections.Add("CustomSection", customSection);
customSection.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Full);
Console.WriteLine("Section name: {0} created",
customSection.SectionInformation.Name);
}
}
catch (ConfigurationErrorsException err)
{
Console.WriteLine(err.ToString());
}
}
The following is the definition of the custom section as used by the previous example.
// Define a custom section. public sealed class CustomSection : ConfigurationSection { public enum Permissions { FullControl = 0, Modify = 1, ReadExecute = 2, Read = 3, Write = 4, SpecialPermissions = 5 } public CustomSection() { } [ConfigurationProperty("fileName", DefaultValue = "default.txt")] [StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)] public String FileName { get { return (String)this["fileName"]; } set { this["fileName"] = value; } } [ConfigurationProperty("maxIdleTime", DefaultValue="1:30:30")] public TimeSpan MaxIdleTime { get { return (TimeSpan)this["maxIdleTime"]; } set { this["maxIdleTime"] = value; } } [ConfigurationProperty("permission", DefaultValue = Permissions.Read)] public Permissions Permission { get { return (Permissions)this["permission"]; } set { this["permission"] = value; } } }
The following is a configuration excerpt as used by the previous example.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="CustomSection" type="Samples.AspNet.CustomSection,
Configuration, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null" allowDefinition="Everywhere"
allowExeDefinition="MachineToApplication"
restartOnExternalChanges="true" />
</configSections>
<CustomSection fileName="default.txt" maxIdleTime="01:30:30"
permission="Read" />
</configuration>
Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.
Note: