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;
}
else
{
return 0;
}
Example settings in web.config:
<configuration>
. . .
<applicationSettings>
<ProjectNameSpace.Properties.Settings>
<setting name="mySetting" serializeAs="String">
<value>True</value>
</setting>
</ProjectNameSpace.Properties.Settings>
</applicationSettings>
</configuration>
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?
if(string.IsNullOrEmpty(Properties.Settings.Default.mySetting))
{
// not set in web.config
}
else
{
// 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.