Export (0) Print
Expand All

ConfigurationManager.OpenExeConfiguration Method (ConfigurationUserLevel)

Note: This method is new in the .NET Framework version 2.0.

Opens the configuration file for the current application as a Configuration object.

Namespace: System.Configuration
Assembly: System.Configuration (in system.configuration.dll)

public static Configuration OpenExeConfiguration (
	ConfigurationUserLevel userLevel
)
public static Configuration OpenExeConfiguration (
	ConfigurationUserLevel userLevel
)
public static function OpenExeConfiguration (
	userLevel : ConfigurationUserLevel
) : Configuration

Parameters

userLevel

The ConfigurationUserLevel for which you are opening the configuration.

Return Value

A Configuration object.

Exception typeCondition

ConfigurationErrorsException

A configuration file could not be loaded.

Client applications use a global configuration that applies to all users, separate configurations that apply to individual users, and configurations that apply to roaming users. The userLevel parameter determines the location of the configuration file being opened by indicating whether it has no user level (the configuration file is in the same directory as the application) or has a per-user level (the configuration file is in an application settings path determined by the user level).

Specify which configuration to get by passing one of the following values for userLevel:

  • To get the Configuration object that applies to all users, set userLevel to None.

  • To get the local Configuration object that applies to the current user, set userLevel to PerUserRoamingAndLocal.

  • To get the roaming Configuration object that applies to the current user, set userLevel to PerUserRoaming.

    NoteNote

    To get the Configuration object for a resource, your code must have read privileges on all of 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.

The following code example shows how to use the OpenExeConfiguration method to modify a custom configuration section.

// Modify a custom section. Show how to use the
// OpenExeConfiguration(ConfigurationUserLevel) method.
static void ModifyCustomSection()
{
    // Get the application configuration file.
    System.Configuration.Configuration config =
            ConfigurationManager.OpenExeConfiguration(
            ConfigurationUserLevel.None);

    Console.WriteLine(config.FilePath);

    CustomSection custSection =
       config.Sections[customSectionName] as CustomSection;

    custSection.FileName = "newName.txt";
    custSection.MaxIdleTime = new TimeSpan(0, 15, 0);
    custSection.MaxUsers = custSection.MaxUsers + 10;

    if (!custSection.ElementInformation.IsLocked)
        config.Save();
    else
        Console.WriteLine("Section was locked, could not update.");


Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2015 Microsoft