4

How to ensure that retrieving value from Properties.Settings.Default exists? For example, when I use this code:

folderBrowserDialog1.SelectedPath = (string)Properties.Settings.Default["SelectedPath"];

and value SelectedPath does not exist, I get the followng exception:

System.Configuration.SettingsPropertyNotFoundException' occurred in System.dll

How can I avoid this exception?

David Tansey
  • 5,813
  • 4
  • 35
  • 51
Alex Zhulin
  • 1,239
  • 2
  • 22
  • 42
  • 1
    possible duplicate of [C#: How to make sure a settings variable exists before attempting to use it from another assembly?](http://stackoverflow.com/questions/4647796/c-how-to-make-sure-a-settings-variable-exists-before-attempting-to-use-it-from) – B.K. Nov 09 '14 at 17:37
  • I saw this question but I didn't understand how to use "CommonSettings.Default.ContainsKey(str)". How to get CommonSettings? – Alex Zhulin Nov 09 '14 at 17:47
  • It isn't very clear why you'd write code like this. Use Properties.Settings.Default.SelectedPath and the compiler will tell you that you got it wrong. Which is the point of using the settings designer. – Hans Passant Nov 09 '14 at 18:13
  • Thank you. I wanted to do all things in runtime, like I used with registry in early times. But it seems that I don't have another way than use settings designer – Alex Zhulin Nov 09 '14 at 19:08

4 Answers4

3

Here is a method to check for the existence of a key:

    public static bool PropertiesHasKey(string key)
    {
        foreach (SettingsProperty sp in Properties.Settings.Default.Properties)
        {
            if (sp.Name == key)
            {
                return true;
            }
        }
        return false;
    }
Paul Richards
  • 1,181
  • 1
  • 10
  • 29
  • While the method shown below works, it does so at the cost of an exception. OTOH, the method shown above avoids raising exceptions. – David A. Gray Aug 29 '18 at 04:53
2

Unless that collection provides a method to check whether a given key exists then you will have to wrap the code in a try..catch block.

 try{
     folderBrowserDialog1.SelectedPath = (string)Properties.Settings.Default["SelectedPath"];
 }catch(System.Configuration.SettingsPropertyNotFoundException)
 {
     folderBrowserDialog1.SelectedPath = "";  // or whatever is appropriate in your case
 }

If the Default property implements the IDictionary interface you can use the ContainsKey method to test that the given key exists before trying to access it, like so:

 if(Properties.Settings.Default.ContainsKey("SelectedPath"))
 {
     folderBrowserDialog1.SelectedPath = (string)Properties.Settings.Default["SelectedPath"];
 }else{
     folderBrowserDialog1.SelectedPath = ""; // or whatever else is appropriate in your case
 }
Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
1

try this:(our friend 'Mike Dinescu' mention that with no details - Edit: he has provided details now)

try
{
    folderBrowserDialog1.SelectedPath = 
     (string)Properties.Settings.Default["SelectedPath"]
}
catch(System.Configuration.SettingsPropertyNotFoundException e)
{
    MessageBox.Show(e.Message); // or anything you want
}
catch(Exception e)
{
    //if any exception but above one occurs this part will execute
}

I hope this solution solve your problem :)

Edit : Or without using try catch :

if(!String.IsNullOrEmpty((string)Properties.Settings.Default["SelectedPath"]))
{
   folderBrowserDialog1.SelectedPath = 
         (string)Properties.Settings.Default["SelectedPath"]
}
Arsalan
  • 709
  • 2
  • 14
  • 27
  • Thank you, but is there any way without using try ... catch(System.Configuration.SettingsPropertyNotFoundException e)? – Alex Zhulin Nov 09 '14 at 17:50
0

you can set default null value for the variable . add this code to Settings.Designer.cs file :

[UserScopedSetting]
[DebuggerNonUserCode]
[DefaultSettingValue(null)] // <-- set default value
public string test1
{
    get
    {
        return (string)this[nameof(test1)];
    }
    set
    {
        this[nameof(test1)] = (object)value;
    }
} 

then check :

if (Properties.Settings.Default.test1 != null)
mhKarami
  • 844
  • 11
  • 16