简体   繁体   English

C# 属性.设置.默认

[英]C# Properties.Settings.Default

How to ensure that retrieving value from Properties.Settings.Default exists?如何确保从Properties.Settings.Default检索值存在? 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:并且值SelectedPath不存在,我得到以下异常:

System.Configuration.SettingsPropertyNotFoundException' occurred in System.dll System.Configuration.SettingsPropertyNotFoundException' 发生在 System.dll 中

How can I avoid this exception?我怎样才能避免这个异常?

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;
    }

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..catch块中。

 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:如果Default属性实现了IDictionary接口,您可以使用ContainsKey方法在尝试访问之前测试给定的键是否存在,如下所示:

 if(Properties.Settings.Default.ContainsKey("SelectedPath"))
 {
     folderBrowserDialog1.SelectedPath = (string)Properties.Settings.Default["SelectedPath"];
 }else{
     folderBrowserDialog1.SelectedPath = ""; // or whatever else is appropriate in your case
 }

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 :编辑:或者不使用 try catch :

if(!String.IsNullOrEmpty((string)Properties.Settings.Default["SelectedPath"]))
{
   folderBrowserDialog1.SelectedPath = 
         (string)Properties.Settings.Default["SelectedPath"]
}

you can set default null value for the variable .您可以为变量设置默认null值。 add this code to Settings.Designer.cs file :将此代码添加到Settings.Designer.cs文件:

[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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM