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
}