Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get web.config appSettings as ConfigurationSection not NameValueCollection

Tags:

.net

asp.net

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

like image 649
Bohdan Avatar asked Jun 20 '12 10:06

Bohdan


2 Answers

var configuration = WebConfigurationManager.OpenWebConfiguration("~");
var appSettingsSection = (AppSettingsSection)configuration.GetSection("appSettings");
like image 113
Bohdan Avatar answered Nov 15 '22 23:11

Bohdan


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);
like image 24
nemesv Avatar answered Nov 16 '22 01:11

nemesv