語言

WebConfigurationManager.OpenWebConfiguration 方法

定義

以物件形式開啟網頁應用程式設定檔 Configuration 。

多載

名稱 Description
OpenWebConfiguration(String)

將網頁應用程式設定檔以 Configuration 物件形式開啟,使用指定的虛擬路徑以允許讀寫操作。

OpenWebConfiguration(String, String)

以指定的虛擬路徑與站點名稱開啟網頁應用程式設定檔 Configuration ,作為物件以允許讀寫操作。

OpenWebConfiguration(String, String, String)

以指定的虛擬路徑、站點名稱及位置開啟網頁應用程式設定檔 Configuration ,作為物件以允許讀寫操作。

OpenWebConfiguration(String, String, String, String)

將網頁應用程式設定檔以 Configuration 物件形式開啟,使用指定的虛擬路徑、站點名稱、位置及伺服器,以允許讀寫操作。

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

以物件形式開啟網頁應用程式設定檔 Configuration ,使用指定的虛擬路徑、站點名稱、位置、伺服器及安全上下文,以允許讀寫操作。

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

以物件形式開啟網頁應用程式設定檔 Configuration ,使用指定的虛擬路徑、站點名稱、位置、伺服器及安全上下文,以允許讀寫操作。

OpenWebConfiguration(String)

將網頁應用程式設定檔以 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)

以指定的虛擬路徑與站點名稱開啟網頁應用程式設定檔 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)

以指定的虛擬路徑、站點名稱及位置開啟網頁應用程式設定檔 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)

將網頁應用程式設定檔以 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

網頁應用程式所處伺服器的網路名稱。

傳回

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)

以物件形式開啟網頁應用程式設定檔 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

網頁應用程式所處伺服器的網路名稱。

userToken
IntPtr

nativeint

一個帳戶代幣。

傳回

Configuration 物件。

例外狀況

server或參數userToken無效。

無法載入有效的設定檔。

範例

以下範例說明如何使用此 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

備註

此方法用於透過模擬存取設定檔。

Note

帳號權杖通常從 WindowsIdentity 類別的實例中取得,或透過呼叫非管理程式碼,例如呼叫 Windows API LogonUser。 欲了解更多關於呼叫非受管理程式碼的資訊,請參閱 「消費非受管理 DLL 函式」。

要取得 Configuration 遠端資源的物件,你的程式碼必須在遠端電腦上擁有管理員權限。

另請參閱

適用於

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

以物件形式開啟網頁應用程式設定檔 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

網頁應用程式所處伺服器的網路名稱。

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+ 選項,以啟用遠端電腦上的設定檔存取權限。

另請參閱

適用於