Doomsknight Doomsknight - 27 days ago 5x
C# Question

Check setting exists in web.config

How do I check a setting exists in the web.config file?

I found the following code, however i think it is aimed at an app.config file? where as my settings are in web.config. The below code returns no keys even though there are 6.

if (ConfigurationManager.AppSettings.AllKeys.Contains(settingName))
return 1;
return 0;

Example settings in web.config:

. . .
<setting name="mySetting" serializeAs="String">

Originally i was trying to read it out, and check if it errors or exists.

var property = Properties.Settings.Default.Properties[settingName];

But that line of code seems to load from the web.config, and if it doesn't exist, it gets it from the project settings. So I cant tell if it is in the web.config or not by checking the value is blank, as it is set to something else!


How about setting the design time value to empty value?

  // not set in web.config 
 // set in web.config and use it

Noe that if you set a value for the setting in web.config and later when you open project's settings file, it attempts to synchronize the value to match web.config value.