ConfigurationManager.AppSettings屬性 返回一個NameValueCollection對象,該對象包含當前應用程序的默認配置的AppSettingsSection對象的內容。如何獲取web.config appSettings作爲ConfigurationSection而不是NameValueCollection
,但我需要AppSettingsSection對象,因爲我需要改變它configSource屬性在運行時
ConfigurationManager.AppSettings屬性 返回一個NameValueCollection對象,該對象包含當前應用程序的默認配置的AppSettingsSection對象的內容。如何獲取web.config appSettings作爲ConfigurationSection而不是NameValueCollection
,但我需要AppSettingsSection對象,因爲我需要改變它configSource屬性在運行時
var configuration = WebConfigurationManager.OpenWebConfiguration("~");
var appSettingsSection = (AppSettingsSection)configuration.GetSection("appSettings");
您可以用Configuration.GetSection方法或與Configuration.AppSetting財產得到AppSettingsSection
。
爲了獲得Configuration
對象,你需要使用ConfigurationManager.Open...
或WebConfigurationManager.Open...
方法:
string sectionName = "appSettings";
var config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection appSettingSection =
(AppSettingsSection)config .GetSection(sectionName);
無法轉換的類型「System.Configuration.KeyValueInternalCollection」對象鍵入「System.Configuration.AppSettingsSection 」。 – Bohdan
@Bohdan是的,你是對的我已經更新了我的答案。 – nemesv