Share via


WebConfigurationManager.OpenWebConfiguration 方法

定義

開啟 Web 應用程式組態檔做為 Configuration 物件。

多載

OpenWebConfiguration(String)

使用指定的虛擬路徑,開啟 Web 應用程式組態檔做為 Configuration 物件,以允許讀取或寫入作業。

OpenWebConfiguration(String, String)

使用指定的虛擬路徑和網站名稱,開啟 Web 應用程式組態檔做為 Configuration 物件,以允許讀取或寫入作業。

OpenWebConfiguration(String, String, String)

使用指定的虛擬路徑、網站名稱和位置,開啟 Web 應用程式組態檔做為 Configuration 物件,以允許讀取或寫入作業。

OpenWebConfiguration(String, String, String, String)

使用指定的虛擬路徑、網站名稱、位置和伺服器,開啟 Web 應用程式組態檔做為 Configuration 物件,以允許讀取或寫入作業。

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

使用指定的虛擬路徑、網站名稱、位置、伺服器和安全性內容,開啟 Web 應用程式組態檔做為 Configuration 物件,以允許讀取或寫入作業。

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

使用指定的虛擬路徑、網站名稱、位置、伺服器和安全性內容,開啟 Web 應用程式組態檔做為 Configuration 物件,以允許讀取或寫入作業。

OpenWebConfiguration(String)

使用指定的虛擬路徑,開啟 Web 應用程式組態檔做為 Configuration 物件,以允許讀取或寫入作業。

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

參數

path
String

組態檔的虛擬路徑。 如果為 null,則會開啟根 Web.config 檔。

傳回

Configuration 物件。

例外狀況

無法載入有效的組態檔。

範例

下列範例示範如何使用 方法來存取組態資訊 OpenWebConfiguration


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

備註

若要取得 Configuration 資源的 物件,您的程式碼必須具有其繼承設定之所有組態檔的讀取權限。 若要更新組態檔,您的程式碼必須另外具有組態檔及其所在目錄的寫入權限。

另請參閱

適用於

OpenWebConfiguration(String, String)

使用指定的虛擬路徑和網站名稱,開啟 Web 應用程式組態檔做為 Configuration 物件,以允許讀取或寫入作業。

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

參數

path
String

組態檔的虛擬路徑。

site
String

Internet Information Services (IIS) 組態中顯示的應用程式網站名稱。

傳回

Configuration 物件。

例外狀況

無法載入有效的組態檔。

範例

下列範例示範如何使用 方法來存取組態資訊 OpenWebConfiguration


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

備註

若要取得 Configuration 資源的 物件,您的程式碼必須具有其繼承設定之所有組態檔的讀取權限。 若要更新組態檔,您的程式碼必須另外具有組態檔及其所在目錄的寫入權限。

另請參閱

適用於

OpenWebConfiguration(String, String, String)

使用指定的虛擬路徑、網站名稱和位置,開啟 Web 應用程式組態檔做為 Configuration 物件,以允許讀取或寫入作業。

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

參數

path
String

組態檔的虛擬路徑。

site
String

Internet Information Services (IIS) 組態中顯示的應用程式網站名稱。

locationSubPath
String

套用此組態的特定資源。

傳回

Configuration 物件。

例外狀況

無法載入有效的組態檔。

範例

下列範例示範如何使用 方法來存取組態資訊 OpenWebConfiguration


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

備註

若要取得 Configuration 資源的 物件,您的程式碼必須具有其繼承設定之所有組態檔的讀取權限。 若要更新組態檔,您的程式碼必須另外具有組態檔及其所在目錄的寫入權限。

另請參閱

適用於

OpenWebConfiguration(String, String, String, String)

使用指定的虛擬路徑、網站名稱、位置和伺服器,開啟 Web 應用程式組態檔做為 Configuration 物件,以允許讀取或寫入作業。

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

參數

path
String

組態檔的虛擬路徑。

site
String

Internet Information Services (IIS) 組態中顯示的應用程式網站名稱。

locationSubPath
String

套用此組態的特定資源。

server
String

Web 應用程式所在之伺服器的網路名稱。

傳回

Configuration 物件。

例外狀況

伺服器參數無效。

無法載入有效的組態檔。

範例

下列範例示範如何使用 方法來存取組態資訊 OpenWebConfiguration


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

備註

若要取得 Configuration 遠端資源的 物件,您的程式碼必須在遠端電腦上具有系統管理許可權。

另請參閱

適用於

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

使用指定的虛擬路徑、網站名稱、位置、伺服器和安全性內容,開啟 Web 應用程式組態檔做為 Configuration 物件,以允許讀取或寫入作業。

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

參數

path
String

組態檔的虛擬路徑。

site
String

Internet Information Services (IIS) 組態中顯示的應用程式網站名稱。

locationSubPath
String

套用此組態的特定資源。

server
String

Web 應用程式所在之伺服器的網路名稱。

userToken
IntPtr

nativeint

要使用的帳戶語彙基元。

傳回

Configuration 物件。

例外狀況

serveruserToken 參數無效。

無法載入有效的組態檔。

範例

下列範例示範如何使用 OpenWebConfiguration 方法來存取組態資訊。


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

備註

這個方法可用來使用模擬來存取組態檔。

注意

帳戶權杖通常是從 類別的 WindowsIdentity 實例擷取,或透過對 Unmanaged 程式碼的呼叫來擷取,例如 Windows API LogonUser 的呼叫。 如需對 Unmanaged 程式碼呼叫的詳細資訊,請參閱 取用 Unmanaged DLL 函式

若要取得 Configuration 遠端資源的 物件,您的程式碼必須在遠端電腦上具有系統管理許可權。

另請參閱

適用於

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

使用指定的虛擬路徑、網站名稱、位置、伺服器和安全性內容,開啟 Web 應用程式組態檔做為 Configuration 物件,以允許讀取或寫入作業。

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

參數

path
String

組態檔的虛擬路徑。

site
String

Internet Information Services (IIS) 組態中顯示的應用程式網站名稱。

locationSubPath
String

套用此組態的特定資源。

server
String

Web 應用程式所在之伺服器的網路名稱。

userName
String

開啟檔案時使用的完整使用者名稱 (Domain\User)。

password
String

使用者名稱的密碼。

傳回

Configuration 物件。

例外狀況

serveruserNamepassword 參數無效。

無法載入有效的組態檔。

範例

下列範例示範如何使用 方法來存取組態資訊 OpenWebConfiguration


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

備註

這個方法可用來使用模擬來存取組態檔。

若要取得 Configuration 遠端資源的 物件,您的程式碼必須在遠端電腦上具有系統管理許可權。

您可能需要使用 選項來執行 ASP.NET IIS 註冊工具 (Aspnet_regiis.exe) -config+ ,以啟用遠端電腦上的組態檔存取權。

另請參閱

適用於