2012-06-20 6 views
9

ConfigurationManager.AppSettingsプロパティ 現在のアプリケーションの既定の構成のAppSettingsSectionオブジェクトの内容を含むNameValueCollectionオブジェクトを返します。NameValueCollectionでないConfigurationSectionとしてweb.config appSettingsを取得する方法

が、私はあなたがConfiguration.GetSectionメソッドまたはConfiguration.AppSettingプロパティでAppSettingsSectionを得ることができ、それを変更するには、ランタイム

答えて

5
var configuration = WebConfigurationManager.OpenWebConfiguration("~"); 
var appSettingsSection = (AppSettingsSection)configuration.GetSection("appSettings"); 
3

でconfigSourceプロパティを必要とするので、私はAppSettingsSectionオブジェクトが必要です。

string sectionName = "appSettings"; 
var config = 
    ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
AppSettingsSection appSettingSection = 
    (AppSettingsSection)config .GetSection(sectionName); 
+2

入力するタイプ「System.Configuration.KeyValueInternalCollection」のオブジェクトをキャストできません「System.Configuration.AppSettingsSection:

あなたはConfigurationManager.Open...WebConfigurationManager.Open...メソッドを使用する必要がConfigurationオブジェクトを取得するには' – Bohdan

+0

@Bohdanはい私の答えを更新しました。 – nemesv

関連する問題