WebConfigurationManager WebConfigurationManager WebConfigurationManager WebConfigurationManager Class

定義

在將組態檔套用至 Web 應用程式時提供對它們的存取權。Provides access to configuration files as they apply to Web applications.

public ref class WebConfigurationManager abstract sealed
public static class WebConfigurationManager
type WebConfigurationManager = class
Public Class WebConfigurationManager
繼承
WebConfigurationManagerWebConfigurationManagerWebConfigurationManagerWebConfigurationManager

範例

下列範例顯示如何使用來存取設定資訊The following example shows how to access configuration information with the

GetSection 方法。GetSection method.


// Show how to use the GetSection(string). 
// to access the connectionStrings section.
static void GetConnectionStringsSection()
{

    // Get the connectionStrings section.
    ConnectionStringsSection connectionStringsSection =
        WebConfigurationManager.GetSection("connectionStrings")
        as ConnectionStringsSection;

    // Get the connectionStrings key,value pairs collection.
    ConnectionStringSettingsCollection connectionStrings =
        connectionStringsSection.ConnectionStrings;
   
    // Get the collection enumerator.
    IEnumerator connectionStringsEnum =
        connectionStrings.GetEnumerator();

    // Loop through the collection and 
    // display the connectionStrings key, value pairs.
    int i = 0;
    Console.WriteLine("[Display the connectionStrings]");
    while (connectionStringsEnum.MoveNext())
    {
        string name = connectionStrings[i].Name;
        Console.WriteLine("Name: {0} Value: {1}",
        name, connectionStrings[name]);
        i += 1;
    }

    Console.WriteLine();
}

' Show how to use the GetSection(string). 
' to access the connectionStrings section.
Shared Sub GetConnectionStringsSection()
   
   ' Get the connectionStrings section.
     Dim connectionStringsSection As ConnectionStringsSection = _
     WebConfigurationManager.GetSection("connectionStrings")
   
   ' Get the connectionStrings key,value pairs collection.
     Dim connectionStrings As ConnectionStringSettingsCollection = _
     connectionStringsSection.ConnectionStrings
   
   ' Get the collection enumerator.
     Dim connectionStringsEnum As IEnumerator = _
     connectionStrings.GetEnumerator()
   
   ' Loop through the collection and 
   ' display the connectionStrings key, value pairs.
   Dim i As Integer = 0
   Console.WriteLine("[Display the connectionStrings]")
   While connectionStringsEnum.MoveNext()
      Dim name As String = connectionStrings(i).Name
         Console.WriteLine("Name: {0} Value: {1}", _
         name, connectionStrings(name))
      i += 1
   End While
   
   Console.WriteLine()
End Sub


備註

WebConfigurationManager類別可讓您存取電腦和應用程式資訊。The WebConfigurationManager class allows you to access computer and application information.

使用WebConfigurationManager是處理與 Web 應用程式相關之設定檔的慣用方式。Using WebConfigurationManager is the preferred way to work with configuration files related to Web applications. 若為用戶端應用程式ConfigurationManager , 請使用類別。For client applications, use the ConfigurationManager class.

您的應用程式可以System.Configuration擴充類型, 或直接使用它們來處理設定資訊, 如下列清單所述:Your application can extend the System.Configuration types or use them directly to handle configuration information, as explained in the following list:

給繼承者的注意事項

Configuration類別可讓您以程式設計方式存取編輯設定檔。The Configuration class allows programmatic access for editing configuration files. 您可以使用所提供WebConfigurationManager的其中一個 open 方法。You use one of the open methods provided by WebConfigurationManager. 這些方法會Configuration傳回物件, 接著提供必要的方法和屬性來處理基礎的設定檔。These methods will return a Configuration object, which in turn provides the required methods and properties to handle the underlying configuration files. 您可以存取這些檔案以進行讀取或寫入, 如下所示:You can access these files for reading or writing as follows: 您可以GetSection(String)使用GetSectionGroup(String)或來讀取設定資訊。You use GetSection(String) or GetSectionGroup(String) to read configuration information. 請注意, 讀取的使用者或處理常式必須具有下列許可權:Note that the user or process that reads must have the following permissions: -位於目前設定階層層級之設定檔的讀取權限。- Read permission on the configuration file at the current configuration hierarchy level.

-所有父設定檔的讀取權限。- Read permissions on all the parent configuration files.

如果您的應用程式需要唯讀存取自己的設定, 建議您使用GetSection方法。If your application needs read-only access to its own configuration, it is recommended you use the GetSection methods. 這些方法可讓您存取目前應用程式的快取設定值, 其效能高於Configuration類別。These methods provide access to the cached configuration values for the current application, which has better performance than the Configuration class.


如果您使用path接受參數GetSection的靜態方法, path 參數必須參考程式碼執行所在的應用程式; 否則, 會忽略參數, 以及目前正在執行之的設定資訊。傳回應用程式。If you use a static GetSection method that takes a path parameter, the path parameter must refer to the application in which the code is running; otherwise, the parameter is ignored and configuration information for the currently-running application is returned.

您可以使用其中一Save種方法來寫入設定資訊。You use one of the Save methods to write configuration information. 請注意, 寫入的使用者或處理常式必須具有下列許可權:Note that the user or process that writes must have the following permissions: -設定檔案和目錄 (位於目前設定階層層級) 的寫入權限。- Write permission on the configuration file and directory at the current configuration hierarchy level. -所有設定檔的讀取權限。- Read permissions on all the configuration files.

屬性

AppSettings AppSettings AppSettings AppSettings

取得網站的應用程式設定。Gets the Web site's application settings.

ConnectionStrings ConnectionStrings ConnectionStrings ConnectionStrings

取得網站的連接字串。Gets the Web site's connection strings.

方法

GetSection(String) GetSection(String) GetSection(String) GetSection(String)

從目前 Web 應用程式的組態檔中擷取指定的組態區段。Retrieves the specified configuration section from the current Web application's configuration file.

GetSection(String, String) GetSection(String, String) GetSection(String, String) GetSection(String, String)

在指定的位置從 Web 應用程式的組態檔擷取指定的組態區段。Retrieves the specified configuration section from the Web application's configuration file at the specified location.

GetWebApplicationSection(String) GetWebApplicationSection(String) GetWebApplicationSection(String) GetWebApplicationSection(String)

從目前 Web 應用程式的組態檔中擷取指定的組態區段。Retrieves the specified configuration section from the current Web application's configuration file.

OpenMachineConfiguration() OpenMachineConfiguration() OpenMachineConfiguration() OpenMachineConfiguration()

開啟目前電腦上的電腦組態檔做為 Configuration 物件,以允許讀取或寫入作業。Opens the machine-configuration file on the current computer as a Configuration object to allow read or write operations.

OpenMachineConfiguration(String) OpenMachineConfiguration(String) OpenMachineConfiguration(String) OpenMachineConfiguration(String)

開啟目前電腦上的電腦組態檔做為 Configuration 物件,以允許讀取或寫入作業。Opens the machine-configuration file on the current computer as a Configuration object to allow read or write operations.

OpenMachineConfiguration(String, String) OpenMachineConfiguration(String, String) OpenMachineConfiguration(String, String) OpenMachineConfiguration(String, String)

開啟指定伺服器上的指定電腦組態檔做為 Configuration 物件,以允許讀取或寫入作業。Opens the specified machine-configuration file on the specified server as a Configuration object to allow read or write operations.

OpenMachineConfiguration(String, String, IntPtr) OpenMachineConfiguration(String, String, IntPtr) OpenMachineConfiguration(String, String, IntPtr) OpenMachineConfiguration(String, String, IntPtr)

使用指定的安全性內容,開啟指定伺服器上的指定電腦組態檔做為 Configuration 物件,以允許讀取或寫入作業。Opens the specified machine-configuration file on the specified server as a Configuration object, using the specified security context to allow read or write operations.

OpenMachineConfiguration(String, String, String, String) OpenMachineConfiguration(String, String, String, String) OpenMachineConfiguration(String, String, String, String) OpenMachineConfiguration(String, String, String, String)

使用指定的安全性內容,開啟指定伺服器上的指定電腦組態檔做為 Configuration 物件,以允許讀取或寫入作業。Opens the specified machine-configuration file on the specified server as a Configuration object, using the specified security context to allow read or write operations.

OpenMappedMachineConfiguration(ConfigurationFileMap, String) OpenMappedMachineConfiguration(ConfigurationFileMap, String) OpenMappedMachineConfiguration(ConfigurationFileMap, String) OpenMappedMachineConfiguration(ConfigurationFileMap, String)

使用指定的檔案對應和位置,開啟電腦組態檔做為 Configuration 物件,以允許讀取或寫入作業。Opens the machine-configuration file as a Configuration object using the specified file mapping and location to allow read or write operations.

OpenMappedMachineConfiguration(ConfigurationFileMap) OpenMappedMachineConfiguration(ConfigurationFileMap) OpenMappedMachineConfiguration(ConfigurationFileMap) OpenMappedMachineConfiguration(ConfigurationFileMap)

使用指定的檔案對應,開啟電腦組態檔做為 Configuration 物件,以允許讀取或寫入作業。Opens the machine-configuration file as a Configuration object, using the specified file mapping to allow read or write operations.

OpenMappedWebConfiguration(WebConfigurationFileMap, String) OpenMappedWebConfiguration(WebConfigurationFileMap, String) OpenMappedWebConfiguration(WebConfigurationFileMap, String) OpenMappedWebConfiguration(WebConfigurationFileMap, String)

使用指定的檔案對應和虛擬路徑,開啟指定的 Web 應用程式組態檔做為 Configuration 物件,以允許讀取或寫入作業。Opens the specified Web-application configuration file as a Configuration object using the specified file mapping and virtual path to allow read or write operations.

OpenMappedWebConfiguration(WebConfigurationFileMap, String, String) OpenMappedWebConfiguration(WebConfigurationFileMap, String, String) OpenMappedWebConfiguration(WebConfigurationFileMap, String, String) OpenMappedWebConfiguration(WebConfigurationFileMap, String, String)

使用指定的檔案對應、虛擬路徑和網站名稱,開啟指定的 Web 應用程式組態檔做為 Configuration 物件,以允許讀取或寫入作業。Opens the specified Web application configuration file as a Configuration object using the specified file mapping, virtual path, and site name to allow read or write operations.

OpenMappedWebConfiguration(WebConfigurationFileMap, String, String, String) OpenMappedWebConfiguration(WebConfigurationFileMap, String, String, String) OpenMappedWebConfiguration(WebConfigurationFileMap, String, String, String) OpenMappedWebConfiguration(WebConfigurationFileMap, String, String, String)

使用指定的檔案對應、虛擬路徑、網站名稱和位置,開啟指定的 Web 應用程式組態檔做為 Configuration 物件,以允許讀取或寫入作業。Opens the specified Web-application configuration file as a Configuration object using the specified file mapping, virtual path, site name, and location to allow read or write operations.

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

使用指定的虛擬路徑,開啟 Web 應用程式組態檔做為 Configuration 物件,以允許讀取或寫入作業。Opens the Web-application configuration file as a Configuration object using the specified virtual path to allow read or write operations.

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

使用指定的虛擬路徑和網站名稱,開啟 Web 應用程式組態檔做為 Configuration 物件,以允許讀取或寫入作業。Opens the Web-application configuration file as a Configuration object using the specified virtual path and site name to allow read or write operations.

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

使用指定的虛擬路徑、網站名稱和位置,開啟 Web 應用程式組態檔做為 Configuration 物件,以允許讀取或寫入作業。Opens the Web-application configuration file as a Configuration object using the specified virtual path, site name, and location to allow read or write operations.

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

使用指定的虛擬路徑、網站名稱、位置和伺服器,開啟 Web 應用程式組態檔做為 Configuration 物件,以允許讀取或寫入作業。Opens the Web-application configuration file as a Configuration object using the specified virtual path, site name, location, and server to allow read or write operations.

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

使用指定的虛擬路徑、網站名稱、位置、伺服器和安全性內容,開啟 Web 應用程式組態檔做為 Configuration 物件,以允許讀取或寫入作業。Opens the Web-application configuration file as a Configuration object using the specified virtual path, site name, location, server, and security context to allow read or write operations.

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

使用指定的虛擬路徑、網站名稱、位置、伺服器和安全性內容,開啟 Web 應用程式組態檔做為 Configuration 物件,以允許讀取或寫入作業。Opens the Web-application configuration file as a Configuration object using the specified virtual path, site name, location, server, and security context to allow read or write operations.

適用於

另請參閱