ConfigurationManager.AppSettings 屬性
注意:此屬性在 .NET Framework 2.0 版中是新增的。
命名空間:System.Configuration
程式集:System.Configuration(在 system.configuration.dll 中)
文法Visual Basic(聲明)
Public Shared ReadOnly Property AppSettings As NameValueCollection
Visual Basic(用法)
Dim value As NameValueCollectionvalue = ConfigurationManager.AppSettings
C#
public static NameValueCollection AppSettings { get; }
C++
public:static property NameValueCollection^ AppSettings { NameValueCollection^ get ();}
J#
/** @property */public static NameValueCollection get_AppSettings ()
JScript
public static function get AppSettings () : NameValueCollection
屬性值
返回一個 NameValueCollection 對象,該對象包含當前應用程式預設配置的 AppSettingsSection 對象的內容。 異常
異常類型 |
條件 |
ConfigurationErrorsException |
無法使用應用程式設定資料檢索 NameValueCollection 對象。 |
備忘
AppSettingsSection 對象包含設定檔的 appSettings 節的內容。
樣本
下面的程式碼範例示範如何使用 AppSettings 屬性擷取命名的應用程式設定。
Visual Basic
' Get appSettings.Shared Sub GetAppSettings() ' Get the appSettings. Dim appSettings As NameValueCollection = _ ConfigurationManager.AppSettings ' Get the collection enumerator. Dim appSettingsEnum As IEnumerator = _ appSettings.Keys.GetEnumerator() ' Loop through the collection and ' display the appSettings key, value pairs. Dim i As Integer = 0 While appSettingsEnum.MoveNext() Dim key As String = appSettings.Keys(i) Console.WriteLine("Name: {0} Value: {1}", _ key, appSettings(key)) i += 1 End WhileEnd Sub 'GetAppSettings
C#
// Get appSettings.static void GetAppSettings(){ // Get the appSettings. NameValueCollection appSettings = ConfigurationManager.AppSettings; // Get the collection enumerator. IEnumerator appSettingsEnum = appSettings.Keys.GetEnumerator(); // Loop through the collection and // display the appSettings key, value pairs. int i = 0; Console.WriteLine("App settings."); while (appSettingsEnum.MoveNext()) { string key = appSettings.Keys[i]; Console.WriteLine("Name: {0} Value: {1}", key, appSettings[key]); i += 1; }}