.NET Framework Class Library
WebConfigurationManager..::.OpenWebConfiguration Method (String)

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

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

Visual Basic (Declaration)
Public Shared Function OpenWebConfiguration ( _
    path As String _
) As Configuration
Visual Basic (Usage)
Dim path As String
Dim returnValue As Configuration

returnValue = WebConfigurationManager.OpenWebConfiguration(path)
C#
public static Configuration OpenWebConfiguration(
    string path
)
Visual C++
public:
static Configuration^ OpenWebConfiguration(
    String^ path
)
JScript
public static function OpenWebConfiguration(
    path : String
) : Configuration

Parameters

path
Type: System..::.String
The virtual path to the configuration file. If nullNothingnullptra null reference (Nothing in Visual Basic), the root Web.config file is opened.
Exceptions

ExceptionCondition
ConfigurationErrorsException

A valid configuration file could not be loaded.

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.

TopicLocation
How to: Access ASP.NET Configuration Settings ProgrammaticallyConfiguring ASP .NET Web Applications
How to: Access ASP.NET Configuration Settings ProgrammaticallyConfiguring ASP .NET Web Applications
How to: Access ASP.NET Configuration Settings ProgrammaticallyBuilding ASP .NET Web Applications in Visual Studio
How to: Access ASP.NET Configuration Settings ProgrammaticallyBuilding ASP .NET Web Applications in Visual Studio
How to: Lock ASP.NET Configuration SettingsConfiguring ASP .NET Web Applications
How to: Lock ASP.NET Configuration SettingsConfiguring ASP .NET Web Applications
How to: Lock ASP.NET Configuration SettingsBuilding ASP .NET Web Applications in Visual Studio
How to: Lock ASP.NET Configuration SettingsBuilding ASP .NET Web Applications in Visual Studio
How to: Read Application Settings from the Web.config FileConfiguring ASP .NET Web Applications
How to: Read Application Settings from the Web.config FileConfiguring ASP .NET Web Applications
How to: Read Application Settings from the Web.config FileBuilding ASP .NET Web Applications in Visual Studio
How to: Read Application Settings from the Web.config FileBuilding ASP .NET Web Applications in Visual Studio
How to: View Inherited and Local Configuration Settings ProgrammaticallyConfiguring ASP .NET Web Applications
How to: View Inherited and Local Configuration Settings ProgrammaticallyConfiguring ASP .NET Web Applications
How to: View Inherited and Local Configuration Settings ProgrammaticallyBuilding ASP .NET Web Applications in Visual Studio
How to: View Inherited and Local Configuration Settings ProgrammaticallyBuilding ASP .NET Web Applications in Visual Studio
Examples

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

Visual Basic
' 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 'OpenWebConfiguration1


C#
// 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();
}

Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0
See Also

Reference

Tags :


Community Content

NewKiwi
Use WebConfigurationManager.GetSection static to get section from running web.config

The most common thing I want to do is to open and read from the Configuration file of the currently executing ASP.NET application. Unfortunately most of the examples they give you of openning and reading from the configuration file use some variation of

Configuration config = WebConfigurationManager.OpenWebConfiguration("SomeString");
 ConfigSection section = config.GetSection("somepath");

There is a note that the null or empty string on the OpenWebConfiguration will open the current application config. This is not necessarily true.

If you want to get to a section in the config file for the currently loaded web app, you can "skip" the entire OpenWebConfiguration method altogether and use instead the static GetSection method on the WebConfigurationManager (which DOES retrieve from the current application config file).

CustomErrorsSection customErrors = WebConfigurationManager.GetSection( "system.web/customErrors" ) as CustomErrorsSection;

jtom
This is what you need to use to get the current web.config settings

It took me a little while to get the WebConfigurtionManager to work for me. I wanted to get the Current sites Web.Config file but kept getting the default sites as I was not using the correct OpenWebConfiguration("SOMETHING") string. This si what you need to use to do it.... [c# example below]

System.Configuration.

Configuration oConfig;

string

path = Request.CurrentExecutionFilePath;

path = path.Substring(0, path.LastIndexOf("/"));

oConfig = System.Web.Configuration.

WebConfigurationManager.OpenWebConfiguration(path);

Lets say that you want to get a connection string (called "Live" in this example) from the web.config file loaded into the oConfig object above then you would do the following:

string

strConnectionString;

strConnectionString = oConfig.ConnectionStrings.ConnectionStrings[

"Live"].ConnectionString;

Thomas Lee
OpenExeConfiguration for non web
Use System.Configuration.ConfigurationManager.OpenExeConfiguration non web.

example:

System.Configuration.ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None).Sections["CustomSection"]

Page view tracker