WebConfigurationManager 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供適用於網頁應用程式的設定檔存取。
public ref class WebConfigurationManager abstract sealed
public static class WebConfigurationManager
type WebConfigurationManager = class
Public Class WebConfigurationManager
- 繼承
-
WebConfigurationManager
範例
以下範例展示了如何透過
GetSection 方法。
// 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 允許你存取電腦及應用程式資訊。
使用 WebConfigurationManager 是處理與網頁應用程式相關設定檔的首選方式。 對於客戶端應用,使用這個 ConfigurationManager 類別。
您的應用程式可以擴充這些 System.Configuration 型別,或直接使用它們來處理設定資訊,詳見以下清單:
Handling configuration。 若要使用標準型別處理組態資訊,請採用以下方法之一:Accessing a section。 要存取應用程式的設定資訊,您必須使用GetSection以下方法之一。WebConfigurationManager 對於<appSettings>和<connectionStrings>,你使用 AppSettings 和 ConnectionStrings 屬性。 這些方法執行唯讀操作,使用單一快取的配置實例,且具備多執行緒感知能力。Accessing configuration files。 您的應用程式可以在任何層級、本身或其他應用程式或計算機、本機或遠端讀取和寫入組態設定。 您可以使用open所提供的其中一個 WebConfigurationManager 方法。 這些方法會回傳一個 Configuration 物件,而物件則提供處理底層設定檔所需的方法與屬性。 這些方法執行讀寫操作,並在每次開啟檔案時重建設定資料。Advanced configuration。 更進階的配置處理方式包括類型 SectionInformation、 PropertyInformation、 PropertyInformationCollection、 ElementInformationContextInformationConfigurationSectionGroupConfigurationSectionGroupCollection和 。
Extending configuration standard types。 你也可以透過擴展標準配置類型 ConfigurationElement如 、 ConfigurationElementCollection、 ConfigurationProperty,以及 ConfigurationSection 使用程式化或屬性化模型來提供自訂配置元素。 請參考該 ConfigurationSection 類別,了解如何以程式方式擴展標準配置型態。 請參考該 ConfigurationElement 類別,了解如何使用屬性模型來擴充標準組態型別。
給繼承者的注意事項
該 Configuration 類別允許程式化存取以編輯設定檔。 你使用了 的開放方法之一。WebConfigurationManager 這些方法會回傳一個 Configuration 物件,而物件則提供處理底層設定檔所需的方法與屬性。 您可以透過以下方式存取這些檔案進行閱讀或寫入:
你使用 GetSection(String) 或 GetSectionGroup(String) 讀取設定資訊。 請注意,讀取的使用者或程序必須具備以下權限:
讀取目前組態階層層級的設定檔權限。
讀取所有父組設定檔的權限。
如果您的應用程式需要對自身設定的唯讀存取權限,建議您使用這些 GetSection 方法。 這些方法提供存取目前應用程式快取的設定值,該組態的效能優於類別。Configuration
注意:若使用靜GetSection態path參數方法,路徑參數必須指向執行程式碼的應用程式;否則該參數會被忽略,並回傳當前執行應用程式的設定資訊。
你可以用其中一種 Save 方法來寫設定資訊。 請注意,撰寫的使用者或程序必須具備以下權限:
在目前的設定階層層級對設定檔和目錄設定寫入權限。
讀取所有設定檔的權限。
屬性
| 名稱 | Description |
|---|---|
| AppSettings |
取得網站的應用程式設定。 |
| ConnectionStrings |
取得網站的連線字串。 |