Share via


WebConfigurationManager.OpenWebConfiguration Méthode

Définition

Ouvre le fichier de configuration de l'application Web en tant qu'objet Configuration.

Surcharges

OpenWebConfiguration(String)

Ouvre le fichier de configuration de l'application Web en tant qu'objet Configuration, à l'aide du chemin d'accès virtuel spécifié, afin d'autoriser les opérations de lecture ou d'écriture.

OpenWebConfiguration(String, String)

Ouvre le fichier de configuration de l'application Web en tant qu'objet Configuration, à l'aide du chemin d'accès virtuel et du nom du site spécifiés, afin d'autoriser les opérations de lecture ou d'écriture.

OpenWebConfiguration(String, String, String)

Ouvre le fichier de configuration de l'application Web en tant qu'objet Configuration, à l'aide du chemin d'accès virtuel, du nom du site et de l'emplacement spécifiés, afin d'autoriser les opérations de lecture ou d'écriture.

OpenWebConfiguration(String, String, String, String)

Ouvre le fichier de configuration de l'application Web en tant qu'objet Configuration, à l'aide du chemin d'accès virtuel, du nom du site, de l'emplacement et du serveur spécifiés, afin d'autoriser les opérations de lecture ou d'écriture.

OpenWebConfiguration(String, String, String, String, IntPtr)

Ouvre le fichier de configuration de l'application Web en tant qu'objet Configuration, à l'aide du chemin d'accès virtuel, du nom du site, de l'emplacement, du serveur et du contexte de sécurité spécifiés, afin d'autoriser les opérations de lecture ou d'écriture.

OpenWebConfiguration(String, String, String, String, String, String)

Ouvre le fichier de configuration de l'application Web en tant qu'objet Configuration, à l'aide du chemin d'accès virtuel, du nom du site, de l'emplacement, du serveur et du contexte de sécurité spécifiés, afin d'autoriser les opérations de lecture ou d'écriture.

OpenWebConfiguration(String)

Ouvre le fichier de configuration de l'application Web en tant qu'objet Configuration, à l'aide du chemin d'accès virtuel spécifié, afin d'autoriser les opérations de lecture ou d'écriture.

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

Paramètres

path
String

Chemin d'accès virtuel du fichier de configuration. Si null, le fichier racine Web.config est ouvert.

Retours

Objet Configuration.

Exceptions

Un fichier de configuration valide n'a pas pu être chargé.

Exemples

L’exemple suivant montre comment accéder aux informations de configuration avec la OpenWebConfiguration méthode .


// 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

Remarques

Pour obtenir l’objet Configuration d’une ressource, votre code doit disposer de privilèges de lecture sur tous les fichiers de configuration dont il hérite des paramètres. Pour mettre à jour un fichier de configuration, votre code doit en outre disposer de privilèges d’écriture pour le fichier de configuration et le répertoire dans lequel il existe.

Voir aussi

S’applique à

OpenWebConfiguration(String, String)

Ouvre le fichier de configuration de l'application Web en tant qu'objet Configuration, à l'aide du chemin d'accès virtuel et du nom du site spécifiés, afin d'autoriser les opérations de lecture ou d'écriture.

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

Paramètres

path
String

Chemin d'accès virtuel du fichier de configuration.

site
String

Nom du site Web de l'application tel qu'affiché dans la configuration des services IIS (Internet Information Services).

Retours

Objet Configuration.

Exceptions

Un fichier de configuration valide n'a pas pu être chargé.

Exemples

L’exemple suivant montre comment accéder aux informations de configuration avec la OpenWebConfiguration méthode .


// 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

Remarques

Pour obtenir l’objet Configuration d’une ressource, votre code doit disposer de privilèges de lecture sur tous les fichiers de configuration dont il hérite des paramètres. Pour mettre à jour un fichier de configuration, votre code doit en outre disposer de privilèges d’écriture pour le fichier de configuration et le répertoire dans lequel il existe.

Voir aussi

S’applique à

OpenWebConfiguration(String, String, String)

Ouvre le fichier de configuration de l'application Web en tant qu'objet Configuration, à l'aide du chemin d'accès virtuel, du nom du site et de l'emplacement spécifiés, afin d'autoriser les opérations de lecture ou d'écriture.

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

Paramètres

path
String

Chemin d'accès virtuel du fichier de configuration.

site
String

Nom du site Web de l'application tel qu'affiché dans la configuration des services IIS (Internet Information Services).

locationSubPath
String

Ressource spécifique à laquelle la configuration s'applique.

Retours

Objet Configuration.

Exceptions

Un fichier de configuration valide n'a pas pu être chargé.

Exemples

L’exemple suivant montre comment accéder aux informations de configuration avec la OpenWebConfiguration méthode .


// 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

Remarques

Pour obtenir l’objet Configuration d’une ressource, votre code doit disposer de privilèges de lecture sur tous les fichiers de configuration dont il hérite des paramètres. Pour mettre à jour un fichier de configuration, votre code doit en outre disposer de privilèges d’écriture pour le fichier de configuration et le répertoire dans lequel il existe.

Voir aussi

S’applique à

OpenWebConfiguration(String, String, String, String)

Ouvre le fichier de configuration de l'application Web en tant qu'objet Configuration, à l'aide du chemin d'accès virtuel, du nom du site, de l'emplacement et du serveur spécifiés, afin d'autoriser les opérations de lecture ou d'écriture.

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

Paramètres

path
String

Chemin d'accès virtuel du fichier de configuration.

site
String

Nom du site Web de l'application tel qu'affiché dans la configuration des services IIS (Internet Information Services).

locationSubPath
String

Ressource spécifique à laquelle la configuration s'applique.

server
String

Nom de réseau du serveur sur lequel réside l'application Web.

Retours

Objet Configuration.

Exceptions

Le paramètre du serveur n'était pas valide.

Un fichier de configuration valide n'a pas pu être chargé.

Exemples

L’exemple suivant montre comment accéder aux informations de configuration avec la OpenWebConfiguration méthode .


// 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

Remarques

Pour obtenir l’objet Configuration d’une ressource distante, votre code doit disposer de privilèges d’administrateur sur l’ordinateur distant.

Voir aussi

S’applique à

OpenWebConfiguration(String, String, String, String, IntPtr)

Ouvre le fichier de configuration de l'application Web en tant qu'objet Configuration, à l'aide du chemin d'accès virtuel, du nom du site, de l'emplacement, du serveur et du contexte de sécurité spécifiés, afin d'autoriser les opérations de lecture ou d'écriture.

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

Paramètres

path
String

Chemin d'accès virtuel du fichier de configuration.

site
String

Nom du site Web de l'application tel qu'affiché dans la configuration des services IIS (Internet Information Services).

locationSubPath
String

Ressource spécifique à laquelle la configuration s'applique.

server
String

Nom de réseau du serveur sur lequel réside l'application Web.

userToken
IntPtr

nativeint

Jeton de compte à utiliser.

Retours

Objet Configuration.

Exceptions

Les paramètres server ou userToken n'étaient pas valides.

Un fichier de configuration valide n'a pas pu être chargé.

Exemples

L’exemple suivant montre comment utiliser la OpenWebConfiguration méthode pour accéder aux informations de configuration.


// 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

Remarques

Cette méthode est utilisée pour accéder à un fichier de configuration à l’aide de l’emprunt d’identité.

Notes

Le jeton de compte est généralement récupéré à partir d’une instance de la WindowsIdentity classe ou via un appel à du code non managé, tel qu’un appel à l’API LogonUserWindows . Pour plus d’informations sur les appels à du code non managé, consultez Consommation de fonctions DLL non managées.

Pour obtenir l’objet Configuration d’une ressource distante, votre code doit disposer de privilèges d’administrateur sur l’ordinateur distant.

Voir aussi

S’applique à

OpenWebConfiguration(String, String, String, String, String, String)

Ouvre le fichier de configuration de l'application Web en tant qu'objet Configuration, à l'aide du chemin d'accès virtuel, du nom du site, de l'emplacement, du serveur et du contexte de sécurité spécifiés, afin d'autoriser les opérations de lecture ou d'écriture.

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

Paramètres

path
String

Chemin d'accès virtuel du fichier de configuration.

site
String

Nom du site Web de l'application tel qu'affiché dans la configuration des services IIS (Internet Information Services).

locationSubPath
String

Ressource spécifique à laquelle la configuration s'applique.

server
String

Nom de réseau du serveur sur lequel réside l'application Web.

userName
String

Nom d'utilisateur complet (Domaine\Utilisateur) à utiliser lors de l'ouverture du fichier.

password
String

Mot de passe pour le nom d'utilisateur.

Retours

Objet Configuration.

Exceptions

Les paramètres server ou userName et password n'étaient pas valides.

Impossible de charger un fichier de configuration valide.

Exemples

L’exemple suivant montre comment accéder aux informations de configuration avec la OpenWebConfiguration méthode .


// 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

Remarques

Cette méthode est utilisée pour accéder à un fichier de configuration à l’aide de l’emprunt d’identité.

Pour obtenir l’objet Configuration d’une ressource distante, votre code doit disposer de privilèges d’administrateur sur l’ordinateur distant.

Vous devrez peut-être exécuter l’outil d’inscription IIS ASP.NET (Aspnet_regiis.exe) avec l’option -config+ permettant d’activer l’accès aux fichiers de configuration sur l’ordinateur distant.

Voir aussi

S’applique à