Bohdan
Bohdan

Reputation: 2004

How to get web.config appSettings as ConfigurationSection not NameValueCollection

ConfigurationManager.AppSettings Property Returns a NameValueCollection object that contains the contents of the AppSettingsSection object for the current application's default configuration.

but I need AppSettingsSection object because I need to change it configSource property in runtime

Upvotes: 10

Views: 4053

Answers (2)

Bohdan
Bohdan

Reputation: 2004

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

Upvotes: 5

nemesv
nemesv

Reputation: 139768

You can get the AppSettingsSection with the Configuration.GetSection method or with the Configuration.AppSetting property.

To get an Configuration object you need to use the ConfigurationManager.Open... or the WebConfigurationManager.Open... methods:

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

Upvotes: 4

Related Questions