Επεξεργασία

Κοινή χρήση μέσω


WebConfigurationManager Class

Definition

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

public ref class WebConfigurationManager abstract sealed
public static class WebConfigurationManager
type WebConfigurationManager = class
Public Class WebConfigurationManager
Inheritance
WebConfigurationManager

Examples

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();
}
' Show how to use the GetSection(string). 
' to access the connectionStrings section.
Shared Sub GetConnectionStringsSection()
   
   ' Get the connectionStrings section.
     Dim connectionStringsSection As ConnectionStringsSection = _
     WebConfigurationManager.GetSection("connectionStrings")
   
   ' Get the connectionStrings key,value pairs collection.
     Dim connectionStrings As ConnectionStringSettingsCollection = _
     connectionStringsSection.ConnectionStrings
   
   ' Get the collection enumerator.
     Dim connectionStringsEnum As IEnumerator = _
     connectionStrings.GetEnumerator()
   
   ' Loop through the collection and 
   ' display the connectionStrings key, value pairs.
   Dim i As Integer = 0
   Console.WriteLine("[Display the connectionStrings]")
   While connectionStringsEnum.MoveNext()
      Dim name As String = connectionStrings(i).Name
         Console.WriteLine("Name: {0} Value: {1}", _
         name, connectionStrings(name))
      i += 1
   End While
   
   Console.WriteLine()
End Sub

Remarks

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:

Notes to Inheritors

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(String) or GetSectionGroup(String) 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.

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.

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.

Properties

AppSettings

Gets the Web site's application settings.

ConnectionStrings

Gets the Web site's connection strings.

Methods

GetSection(String, String)

Retrieves the specified configuration section from the Web application's configuration file at the specified location.

GetSection(String)

Retrieves the specified configuration section from the current Web application's configuration file.

GetWebApplicationSection(String)

Retrieves the specified configuration section from the current Web application's configuration file.

OpenMachineConfiguration()

Opens the machine-configuration file on the current computer as a Configuration object to allow read or write operations.

OpenMachineConfiguration(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.

OpenMachineConfiguration(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.

OpenMachineConfiguration(String, String)

Opens the specified machine-configuration file on the specified server as a Configuration object to allow read or write operations.

OpenMachineConfiguration(String)

Opens the machine-configuration file on the current computer as a Configuration object to allow read or write operations.

OpenMappedMachineConfiguration(ConfigurationFileMap, String)

Opens the machine-configuration file as a Configuration object using the specified file mapping and location to allow read or write operations.

OpenMappedMachineConfiguration(ConfigurationFileMap)

Opens the machine-configuration file as a Configuration object, using the specified file mapping to allow read or write operations.

OpenMappedWebConfiguration(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.

OpenMappedWebConfiguration(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.

OpenMappedWebConfiguration(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.

OpenWebConfiguration(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.

OpenWebConfiguration(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.

OpenWebConfiguration(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.

OpenWebConfiguration(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.

OpenWebConfiguration(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.

OpenWebConfiguration(String)

Opens the Web-application configuration file as a Configuration object using the specified virtual path to allow read or write operations.

Applies to

See also