Export (0) Print
Expand All

WebConfigurationManager Class

Provides access to configuration files as they apply to Web applications.

System.Object
  System.Web.Configuration.WebConfigurationManager

Namespace:  System.Web.Configuration
Assembly:  System.Web (in System.Web.dll)

public static class WebConfigurationManager

The WebConfigurationManager type exposes the following members.

  NameDescription
Public propertyStatic memberAppSettingsGets the Web site's application settings.
Public propertyStatic memberConnectionStringsGets the Web site's connection strings.
Top

  NameDescription
Public methodStatic memberGetSection(String)Retrieves the specified configuration section from the current Web application's configuration file.
Public methodStatic memberGetSection(String, String)Retrieves the specified configuration section from the Web application's configuration file at the specified location.
Public methodStatic memberGetWebApplicationSectionRetrieves the specified configuration section from the current Web application's configuration file.
Public methodStatic memberOpenMachineConfiguration()Opens the machine-configuration file on the current computer as a Configuration object to allow read or write operations.
Public methodStatic memberOpenMachineConfiguration(String)Opens the machine-configuration file on the current computer as a Configuration object to allow read or write operations.
Public methodStatic memberOpenMachineConfiguration(String, String)Opens the specified machine-configuration file on the specified server as a Configuration object to allow read or write operations.
Public methodStatic memberOpenMachineConfiguration(String, String, IntPtr)Opens the specified machine-configuration file on the specified server as a Configuration object, using the specified security context to allow read or write operations.
Public methodStatic memberOpenMachineConfiguration(String, String, String, String)Opens the specified machine-configuration file on the specified server as a Configuration object, using the specified security context to allow read or write operations.
Public methodStatic memberOpenMappedMachineConfiguration(ConfigurationFileMap)Opens the machine-configuration file as a Configuration object, using the specified file mapping to allow read or write operations.
Public methodStatic memberOpenMappedMachineConfiguration(ConfigurationFileMap, String)Opens the machine-configuration file as a Configuration object using the specified file mapping and location to allow read or write operations.
Public methodStatic memberOpenMappedWebConfiguration(WebConfigurationFileMap, String)Opens the specified Web-application configuration file as a Configuration object using the specified file mapping and virtual path to allow read or write operations.
Public methodStatic memberOpenMappedWebConfiguration(WebConfigurationFileMap, String, String)Opens the specified Web application configuration file as a Configuration object using the specified file mapping, virtual path, and site name to allow read or write operations.
Public methodStatic memberOpenMappedWebConfiguration(WebConfigurationFileMap, String, String, String)Opens the specified Web-application configuration file as a Configuration object using the specified file mapping, virtual path, site name, and location to allow read or write operations.
Public methodStatic memberOpenWebConfiguration(String)Opens the Web-application configuration file as a Configuration object using the specified virtual path to allow read or write operations.
Public methodStatic memberOpenWebConfiguration(String, String)Opens the Web-application configuration file as a Configuration object using the specified virtual path and site name to allow read or write operations.
Public methodStatic memberOpenWebConfiguration(String, String, String)Opens the Web-application configuration file as a Configuration object using the specified virtual path, site name, and location to allow read or write operations.
Public methodStatic memberOpenWebConfiguration(String, String, String, String)Opens the Web-application configuration file as a Configuration object using the specified virtual path, site name, location, and server to allow read or write operations.
Public methodStatic memberOpenWebConfiguration(String, String, String, String, IntPtr)Opens the Web-application configuration file as a Configuration object using the specified virtual path, site name, location, server, and security context to allow read or write operations.
Public methodStatic memberOpenWebConfiguration(String, String, String, String, String, String)Opens the Web-application configuration file as a Configuration object using the specified virtual path, site name, location, server, and security context to allow read or write operations.
Top

The WebConfigurationManager class allows you to access computer and application information.

Using WebConfigurationManager is the preferred way to work with configuration files related to Web applications. For client applications, use the ConfigurationManager class.

Your application can extend the System.Configuration types or use them directly to handle configuration information, as explained in the following list:

  • Handling configuration. To handle configuration information using the standard types, you use one of the following approaches:

    • Accessing a section. To access configuration information for your application, you must use one of the GetSection methods provided by WebConfigurationManager. For <appSettings> and <connectionStrings>, you use the AppSettings and ConnectionStrings properties. These methods perform read-only operations, use a single cached instance of the configuration, and are multithread aware.

    • Accessing configuration files. Your application can read and write configuration settings at any level, for itself or for other applications or computers, locally or remotely. You use one of the open methods provided by WebConfigurationManager. These methods will return a Configuration object, which in turn provides the required methods and properties to handle the underlying configuration files. These methods perform read or write operations and recreate the configuration data every time a file is opened.

    • Advanced configuration. More advanced configuration handling is provided by the types SectionInformation, PropertyInformation, PropertyInformationCollection, ElementInformation, ContextInformation, ConfigurationSectionGroup, and ConfigurationSectionGroupCollection.

  • Extending configuration standard types. You can also provide your custom configuration elements by extending the standard configuration types such as ConfigurationElement, ConfigurationElementCollection, ConfigurationProperty, and ConfigurationSection by using a programmatic or an attributed model. Refer to the ConfigurationSection class for an example of how to extend a standard configuration type programmatically. Refer to the ConfigurationElement class for an example of how to extend a standard configuration type using the attributed model.

Notes to Implementers

The Configuration class allows programmatic access for editing configuration files. You use one of the open methods provided by WebConfigurationManager. These methods will return a Configuration object, which in turn provides the required methods and properties to handle the underlying configuration files. You can access these files for reading or writing as follows:

You use GetSection or GetSectionGroup to read configuration information. Note that the user or process that reads must have the following permissions:

  • Read permission on the configuration file at the current configuration hierarchy level.

  • Read permissions on all the parent configuration files.

If your application needs read-only access to its own configuration, it is recommended you use the GetSection methods. These methods provide access to the cached configuration values for the current application, which has better performance than the Configuration class.

NoteNote

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.

You use one of the Save methods to write configuration information. Note that the user or process that writes must have the following permissions:

  • Write permission on the configuration file and directory at the current configuration hierarchy level.

  • Read permissions on all the configuration files.

The following example shows how to access configuration information with the

GetSection method.


        // Show how to use the GetSection(string).  
        // to access the connectionStrings section. 
        static void GetConnectionStringsSection()
        {

            // Get the connectionStrings section.
            ConnectionStringsSection connectionStringsSection =
                WebConfigurationManager.GetSection("connectionStrings")
                as ConnectionStringsSection;

            // Get the connectionStrings key,value pairs collection.
            ConnectionStringSettingsCollection connectionStrings =
                connectionStringsSection.ConnectionStrings;

            // Get the collection enumerator.
            IEnumerator connectionStringsEnum =
                connectionStrings.GetEnumerator();

            // Loop through the collection and  
            // display the connectionStrings key, value pairs. 
            int i = 0;
            Console.WriteLine("[Display the connectionStrings]");
            while (connectionStringsEnum.MoveNext())
            {
                string name = connectionStrings[i].Name;
                Console.WriteLine("Name: {0} Value: {1}",
                name, connectionStrings[name]);
                i += 1;
            }

            Console.WriteLine();
        }

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft