WebConfigurationManager.OpenWebConfiguration Method

Definition

Opens the Web-application configuration file as a Configuration object.

Overloads

OpenWebConfiguration(String)

Opens the Web-application configuration file as a Configuration object using the specified virtual path 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, 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, 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, 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)

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

public:
 static System::Configuration::Configuration ^ OpenWebConfiguration(System::String ^ path);
public static System.Configuration.Configuration OpenWebConfiguration (string path);
static member OpenWebConfiguration : string -> System.Configuration.Configuration
Public Shared Function OpenWebConfiguration (path As String) As Configuration

Parameters

path
String

The virtual path to the configuration file. If null, the root Web.config file is opened.

Returns

A Configuration object.

Exceptions

A valid configuration file could not be loaded.

Examples

The following example shows how to access configuration information with the OpenWebConfiguration method.


// Show how to use OpenWebConfiguration(string).
// It gets he appSettings section of a Web application 
// runnig on the local server. 
static void OpenWebConfiguration1()
{
    // Get the configuration object for a Web application
    // running on the local server. 
    System.Configuration.Configuration config =
        WebConfigurationManager.OpenWebConfiguration("/configTest") 
        as System.Configuration.Configuration; 

    // Get the appSettings.
    KeyValueConfigurationCollection appSettings =
         config.AppSettings.Settings;

    // Loop through the collection and
    // display the appSettings key, value pairs.
    Console.WriteLine("[appSettings for app at: {0}]", "/configTest");
    foreach (string key in appSettings.AllKeys)
    {
        Console.WriteLine("Name: {0} Value: {1}",
        key, appSettings[key].Value);
    }

    Console.WriteLine();
}
' Show how to use OpenWebConfiguration(string).
' It gets he appSettings section of a Web application 
' runnig on the local server. 
Shared Sub OpenWebConfiguration1()
   ' Get the configuration object for a Web application
   ' running on the local server. 
     Dim config As System.Configuration.Configuration = _
     WebConfigurationManager.OpenWebConfiguration("/configTest")
   
   ' Get the appSettings.
     Dim appSettings As KeyValueConfigurationCollection = _
     config.AppSettings.Settings
   
   
   ' Loop through the collection and
   ' display the appSettings key, value pairs.
   Console.WriteLine("[appSettings for app at: {0}]", "/configTest")
   Dim key As String
   For Each key In  appSettings.AllKeys
         Console.WriteLine("Name: {0} Value: {1}", _
         key, appSettings(key).Value)
   Next key
   
   Console.WriteLine()
End Sub

Remarks

To obtain the Configuration object for a resource, your code must have read privileges on all the configuration files from which it inherits settings. To update a configuration file, your code must additionally have write privileges for both the configuration file and the directory in which it exists.

See also

Applies to

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.

public:
 static System::Configuration::Configuration ^ OpenWebConfiguration(System::String ^ path, System::String ^ site);
public static System.Configuration.Configuration OpenWebConfiguration (string path, string site);
static member OpenWebConfiguration : string * string -> System.Configuration.Configuration
Public Shared Function OpenWebConfiguration (path As String, site As String) As Configuration

Parameters

path
String

The virtual path to the configuration file.

site
String

The name of the application Web site, as displayed in Internet Information Services (IIS) configuration.

Returns

A Configuration object.

Exceptions

A valid configuration file could not be loaded.

Examples

The following example shows how to access configuration information with the OpenWebConfiguration method.


// Show how to use OpenWebConfiguration(string, string).
// It gets he appSettings section of a Web application 
// runnig on the local server. 
static void OpenWebConfiguration2()
{
    // Get the configuration object for a Web application
    // running on the local server. 
    System.Configuration.Configuration config =
        WebConfigurationManager.OpenWebConfiguration("/configTest", 
        "Default Web Site")
        as System.Configuration.Configuration;

    // Get the appSettings.
    KeyValueConfigurationCollection appSettings =
         config.AppSettings.Settings;

    // Loop through the collection and
    // display the appSettings key, value pairs.
    Console.WriteLine(
        "[appSettings for app at: /configTest");
    Console.WriteLine(" and site: Default Web Site]");

    foreach (string key in appSettings.AllKeys)
    {
        Console.WriteLine("Name: {0} Value: {1}",
        key, appSettings[key].Value);
    }

    Console.WriteLine();
}
' Show how to use OpenWebConfiguration(string, string).
' It gets he appSettings section of a Web application 
' runnig on the local server. 
Shared Sub OpenWebConfiguration2()
   ' Get the configuration object for a Web application
   ' running on the local server. 
     Dim config As System.Configuration.Configuration = _
     WebConfigurationManager.OpenWebConfiguration( _
     "/configTest", "Default Web Site")
   
   ' Get the appSettings.
     Dim appSettings As KeyValueConfigurationCollection = _
     config.AppSettings.Settings
   
   
   ' Loop through the collection and
   ' display the appSettings key, value pairs.
   Console.WriteLine("[appSettings for app at: /configTest")
   Console.WriteLine(" and site: Default Web Site]")
   
   Dim key As String
   For Each key In  appSettings.AllKeys
         Console.WriteLine("Name: {0} Value: {1}", _
         key, appSettings(key).Value)
   Next key
   
   Console.WriteLine()
End Sub

Remarks

To obtain the Configuration object for a resource, your code must have read privileges on all the configuration files from which it inherits settings. To update a configuration file, your code must additionally have write privileges for both the configuration file and the directory in which it exists.

See also

Applies to

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.

public:
 static System::Configuration::Configuration ^ OpenWebConfiguration(System::String ^ path, System::String ^ site, System::String ^ locationSubPath);
public static System.Configuration.Configuration OpenWebConfiguration (string path, string site, string locationSubPath);
static member OpenWebConfiguration : string * string * string -> System.Configuration.Configuration
Public Shared Function OpenWebConfiguration (path As String, site As String, locationSubPath As String) As Configuration

Parameters

path
String

The virtual path to the configuration file.

site
String

The name of the application Web site, as displayed in Internet Information Services (IIS) configuration.

locationSubPath
String

The specific resource to which the configuration applies.

Returns

A Configuration object.

Exceptions

A valid configuration file could not be loaded.

Examples

The following example shows how to access configuration information with the OpenWebConfiguration method.


// Show how to use OpenWebConfiguration(string, string, string).
// It gets he appSettings section of a Web application 
// runnig on the local server. 
static void OpenWebConfiguration3()
{
    // Get the configuration object for a Web application
    // running on the local server. 
    System.Configuration.Configuration config =
        WebConfigurationManager.OpenWebConfiguration(
        "/configTest", "Default Web Site", null)
        as System.Configuration.Configuration;

    // Get the appSettings.
    KeyValueConfigurationCollection appSettings =
         config.AppSettings.Settings;

    // Loop through the collection and
    // display the appSettings key, value pairs.
    Console.WriteLine(
        "[appSettings for app at: /configTest");
    Console.WriteLine(" site: Default Web Site");
    Console.WriteLine(" and locationSubPath: null]");
    
    foreach (string key in appSettings.AllKeys)
    {
        Console.WriteLine("Name: {0} Value: {1}",
        key, appSettings[key].Value);
    }

    Console.WriteLine();
}
' Show how to use OpenWebConfiguration(string, string, string).
' It gets he appSettings section of a Web application 
' runnig on the local server. 
Shared Sub OpenWebConfiguration3()
   ' Get the configuration object for a Web application
   ' running on the local server. 
     Dim config As System.Configuration.Configuration = _
     WebConfigurationManager.OpenWebConfiguration( _
     "/configTest", "Default Web Site", Nothing)
   
   ' Get the appSettings.
     Dim appSettings As KeyValueConfigurationCollection = _
     config.AppSettings.Settings
   
   ' Loop through the collection and
   ' display the appSettings key, value pairs.
   Console.WriteLine("[appSettings for app at: /configTest")
   Console.WriteLine(" site: Default Web Site")
   Console.WriteLine(" and locationSubPath: null]")
   
   Dim key As String
   For Each key In  appSettings.AllKeys
         Console.WriteLine("Name: {0} Value: {1}", _
         key, appSettings(key).Value)
   Next key
   
   Console.WriteLine()
End Sub

Remarks

To obtain the Configuration object for a resource, your code must have read privileges on all the configuration files from which it inherits settings. To update a configuration file, your code must additionally have write privileges for both the configuration file and the directory in which it exists.

See also

Applies to

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.

public:
 static System::Configuration::Configuration ^ OpenWebConfiguration(System::String ^ path, System::String ^ site, System::String ^ locationSubPath, System::String ^ server);
public static System.Configuration.Configuration OpenWebConfiguration (string path, string site, string locationSubPath, string server);
static member OpenWebConfiguration : string * string * string * string -> System.Configuration.Configuration
Public Shared Function OpenWebConfiguration (path As String, site As String, locationSubPath As String, server As String) As Configuration

Parameters

path
String

The virtual path to the configuration file.

site
String

The name of the application Web site, as displayed in Internet Information Services (IIS) configuration.

locationSubPath
String

The specific resource to which the configuration applies.

server
String

The network name of the server the Web application resides on.

Returns

A Configuration object.

Exceptions

The server parameter was invalid.

A valid configuration file could not be loaded.

Examples

The following example shows how to access configuration information with the OpenWebConfiguration method.


// Show how to use OpenWebConfiguration(string, string, 
// string, string).
// It gets he appSettings section of a Web application 
// running on the specified server. 
// If the server is remote your application must have the
// required access rights to the configuration file. 
static void OpenWebConfiguration4()
{
    // Get the configuration object for a Web application
    // running on the specified server.
    // Null for the subPath signifies no subdir. 
    System.Configuration.Configuration config =
           WebConfigurationManager.OpenWebConfiguration(
            "/configTest", "Default Web Site", null, "myServer")
           as System.Configuration.Configuration;
    
    // Get the appSettings.
    KeyValueConfigurationCollection appSettings =
         config.AppSettings.Settings;

    // Loop through the collection and
    // display the appSettings key, value pairs.
    Console.WriteLine("[appSettings for Web app on server: myServer]");
    foreach (string key in appSettings.AllKeys)
    {
        Console.WriteLine("Name: {0} Value: {1}",
        key, appSettings[key].Value);
    }

    Console.WriteLine();
}
' Show how to use OpenWebConfiguration(string, string, 
' string, string).
' It gets he appSettings section of a Web application 
' running on the specified server. 
' If the server is remote your application must have the
' required access rights to the configuration file. 
Shared Sub OpenWebConfiguration4()
   ' Get the configuration object for a Web application
   ' running on the specified server.
   ' Null for the subPath signifies no subdir. 
   Dim config As System.Configuration.Configuration = WebConfigurationManager.OpenWebConfiguration("/configTest", "Default Web Site", Nothing, "myServer")
   
   ' Get the appSettings.
   Dim appSettings As KeyValueConfigurationCollection = config.AppSettings.Settings
   
   
   ' Loop through the collection and
   ' display the appSettings key, value pairs.
   Console.WriteLine("[appSettings for Web app on server: myServer]")
   Dim key As String
   For Each key In  appSettings.AllKeys
      Console.WriteLine("Name: {0} Value: {1}", key, appSettings(key).Value)
   Next key
   
   Console.WriteLine()
End Sub

Remarks

To obtain the Configuration object for a remote resource, your code must have administrative privileges on the remote computer.

See also

Applies to

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.

public:
 static System::Configuration::Configuration ^ OpenWebConfiguration(System::String ^ path, System::String ^ site, System::String ^ locationSubPath, System::String ^ server, IntPtr userToken);
public static System.Configuration.Configuration OpenWebConfiguration (string path, string site, string locationSubPath, string server, IntPtr userToken);
static member OpenWebConfiguration : string * string * string * string * nativeint -> System.Configuration.Configuration
Public Shared Function OpenWebConfiguration (path As String, site As String, locationSubPath As String, server As String, userToken As IntPtr) As Configuration

Parameters

path
String

The virtual path to the configuration file.

site
String

The name of the application Web site, as displayed in Internet Information Services (IIS) configuration.

locationSubPath
String

The specific resource to which the configuration applies.

server
String

The network name of the server the Web application resides on.

userToken
IntPtr

nativeint

An account token to use.

Returns

A Configuration object.

Exceptions

The server or userToken parameters were invalid.

A valid configuration file could not be loaded.

Examples

The following example shows how to use the OpenWebConfiguration method to access configuration information.


// Show how to use OpenWebConfiguration(string, string, 
// string, string, IntPtr).
// It gets he appSettings section of a Web application 
// running on a remote server. 
// If the serve is remote your application shall have the
// requires access rights to the configuration file. 
static void OpenWebConfiguration6()
{

    IntPtr userToken = 
        System.Security.Principal.WindowsIdentity.GetCurrent().Token;
   
    string user = 
        System.Security.Principal.WindowsIdentity.GetCurrent().Name;
    
    // Get the configuration object for a Web application
    // running on a remote server.
    System.Configuration.Configuration config =
        WebConfigurationManager.OpenWebConfiguration(
        "/configTest", "Default Web Site", null, 
        "myServer", userToken) as System.Configuration.Configuration;

    // Get the appSettings.
    KeyValueConfigurationCollection appSettings =
         config.AppSettings.Settings;

    // Loop through the collection and
    // display the appSettings key, value pairs.
    Console.WriteLine(
        "[appSettings for Web app on server: myServer user: {0}]", user);
    foreach (string key in appSettings.AllKeys)
    {
        Console.WriteLine("Name: {0} Value: {1}",
        key, appSettings[key].Value);
    }

    Console.WriteLine();
}
' Show how to use OpenWebConfiguration(string, string, 
' string, string, IntPtr).
' It gets he appSettings section of a Web application 
' running on a remote server. 
' If the serve is remote your application shall have the
' requires access rights to the configuration file. 
Shared Sub OpenWebConfiguration6()
   
     Dim userToken As IntPtr = _
     System.Security.Principal.WindowsIdentity.GetCurrent().Token
   
     Dim user As String = _
     System.Security.Principal.WindowsIdentity.GetCurrent().Name
   
   ' Get the configuration object for a Web application
   ' running on a remote server.
     Dim config As System.Configuration.Configuration = _
     WebConfigurationManager.OpenWebConfiguration( _
     "/configTest", "Default Web Site", _
     Nothing, "myServer", userToken)
   
   ' Get the appSettings.
     Dim appSettings As KeyValueConfigurationCollection = _
     config.AppSettings.Settings
   
   ' Loop through the collection and
   ' display the appSettings key, value pairs.
     Console.WriteLine( _
     "[appSettings for Web app on server: myServer user: {0}]", user)
   Dim key As String
   For Each key In  appSettings.AllKeys
         Console.WriteLine("Name: {0} Value: {1}", _
         key, appSettings(key).Value)
   Next key
   
   Console.WriteLine()
End Sub

Remarks

This method is used to access a configuration file using impersonation.

Note

The account token is usually retrieved from an instance of the WindowsIdentity class or through a call to unmanaged code, such as a call to the Windows API LogonUser. For more information about calls to unmanaged code, see Consuming Unmanaged DLL Functions.

To obtain the Configuration object for a remote resource, your code must have administrative privileges on the remote computer.

See also

Applies to

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.

public:
 static System::Configuration::Configuration ^ OpenWebConfiguration(System::String ^ path, System::String ^ site, System::String ^ locationSubPath, System::String ^ server, System::String ^ userName, System::String ^ password);
public static System.Configuration.Configuration OpenWebConfiguration (string path, string site, string locationSubPath, string server, string userName, string password);
static member OpenWebConfiguration : string * string * string * string * string * string -> System.Configuration.Configuration
Public Shared Function OpenWebConfiguration (path As String, site As String, locationSubPath As String, server As String, userName As String, password As String) As Configuration

Parameters

path
String

The virtual path to the configuration file.

site
String

The name of the application Web site, as displayed in Internet Information Services (IIS) configuration.

locationSubPath
String

The specific resource to which the configuration applies.

server
String

The network name of the server the Web application resides on.

userName
String

The full user name (Domain\User) to use when opening the file.

password
String

The password for the user name.

Returns

A Configuration object.

Exceptions

The server or userName and password parameters were invalid.

Could not load a valid configuration file.

Examples

The following example shows how to access configuration information with the OpenWebConfiguration method.


// Show how to use OpenWebConfiguration(string, string, 
// string, string, string, string).
// It gets he appSettings section of a Web application 
// running on a remote server. 
// If the server is remote your application must have the
// required access rights to the configuration file. 
static void OpenWebConfiguration5()
{
    // Get the current user.
    string user =
        System.Security.Principal.WindowsIdentity.GetCurrent().Name;
    
    // Assign the actual password.
    string password = "userPassword";

    // Get the configuration object for a Web application
    // running on a remote server.
    System.Configuration.Configuration config =
        WebConfigurationManager.OpenWebConfiguration(
        "/configTest", "Default Web Site", null, "myServer",
        user, password) as System.Configuration.Configuration;

    // Get the appSettings.
    KeyValueConfigurationCollection appSettings =
         config.AppSettings.Settings;

    // Loop through the collection and
    // display the appSettings key, value pairs.
    Console.WriteLine(
        "[appSettings for Web app on server: myServer user: {0}]", user);
    foreach (string key in appSettings.AllKeys)
    {
        Console.WriteLine("Name: {0} Value: {1}",
        key, appSettings[key].Value);
    }

    Console.WriteLine();
}
' Show how to use OpenWebConfiguration(string, string, 
' string, string, string, string).
' It gets he appSettings section of a Web application 
' running on a remote server. 
' If the server is remote your application must have the
' required access rights to the configuration file. 
Shared Sub OpenWebConfiguration5()
   ' Get the current user.
     Dim user As String = _
     System.Security.Principal.WindowsIdentity.GetCurrent().Name
   
   ' Assign the actual password.
   Dim password As String = "userPassword"
   
   ' Get the configuration object for a Web application
   ' running on a remote server.
     Dim config As System.Configuration.Configuration = _
     WebConfigurationManager.OpenWebConfiguration( _
     "/configTest", "Default Web Site", _
     Nothing, "myServer", user, password)
   
   ' Get the appSettings.
     Dim appSettings As KeyValueConfigurationCollection = _
     config.AppSettings.Settings
   
   
   ' Loop through the collection and
   ' display the appSettings key, value pairs.
     Console.WriteLine( _
     "[appSettings for Web app on server: myServer user: {0}]", user)
   Dim key As String
   For Each key In  appSettings.AllKeys
         Console.WriteLine("Name: {0} Value: {1}", _
         key, appSettings(key).Value)
   Next key
   
   Console.WriteLine()
End Sub

Remarks

This method is used to access a configuration file using impersonation.

To obtain the Configuration object for a remote resource, your code must have administrative privileges on the remote computer.

You might need to run the ASP.NET IIS Registration Tool (Aspnet_regiis.exe) with the -config+ option to enable access to the configuration files on the remote computer.

See also

Applies to