2

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!

3
  • Is this even valid? Are you not getting errors like "ProjectNameSpace.Properties.Settings is not a valid child element under appsettings"? List of possible elements are add/remove/clear. Typically one would use a customconfigsection like msdn.microsoft.com/en-us/library/2tw134k3.aspx Commented Sep 26, 2016 at 15:20
  • @chandmk Think there is confusion. This is not appsettings. it is appicationsettings. Auto generated from adding to settings in the project properties. It resides directly in configuration in the xml. I struggle to find resources for a solution. they are all appsettings related Commented Sep 26, 2016 at 15:57
  • Right! I misread the applicationsettings. Tried to answer bellow. Commented Sep 26, 2016 at 21:48

4 Answers 4

4

Maybe you can try:

if (ConfigurationManager.AppSettings[name] != null)
{
    //The value exists
}
Sign up to request clarification or add additional context in comments.

1 Comment

OP is not trying to read from AppSettings, they're trying to read from ApplicaitonSettings. Not the same thing.
1

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.

1 Comment

THought i found a better way, based on this. But actually, it must have cached something in my developement build. Setting the values to blank, and ensuring i dont set them auto update seems best way. However there is some code below to also checking settings that are not strings. While taking into account their defaults (they are not null).
0

The following works for me. After much trial and error.

Using the answer above as a basis, Set your parameters in properties to blanks. Do not let them auto update from the config.

Then use similar code to below to check whether the value is empty or not. It will also allow for booleans etc which are set to a value by default, as they cannot be null either way.

You can adapt it accordingly to check a specific setting, as setting name is available in the foreach. Or use ["settingname"].

It works on boolean and string settings. Probably more. It checks that no default is being used (because value not found), and if not, the value contained.

    public int CheckSettings()
    {
        int settings = 0;

        SettingsPropertyValueCollection settingsval = Properties.Settings.Default.PropertyValues;

        foreach (SettingsPropertyValue val in settingsval)
        {
            settings += val.UsingDefaultValue || String.IsNullOrWhiteSpace((string)val.SerializedValue) ? 0 : 1;
        }

        return settings;
    }

Comments

0

For WPF desktop app I would do the following:

if (Settings.Default.Properties[column.ColumnName] != null)
{
   //code                
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.