【问题标题】:Visual Studio 2008 application settings not saved?Visual Studio 2008 应用程序设置未保存?
【发布时间】:2011-06-01 10:42:16
【问题描述】:

我知道:

应用程序设置可以存储为 XML 可序列化或具有实现 ToString/FromString 的 TypeConverter 的任何数据类型。最常见的类型是 String、Integer 和 Boolean,但您也可以将值存储为 Color、Object 或连接字符串。

我有一个 ListDictionary 类设置 - 它是可序列化的,但每次我再次启动我的应用程序时,尽管创建、分配和保存 ListDictionary 到我的设置,它都是 null:

Properties.Settings.Default.Preferences = new System.Collections.Specialized.ListDictionary();
Properties.Settings.Default.Preferences.Add(1, "test");
Properties.Settings.Default.Save();

如何将这样的类用作应用程序设置?我需要一个字典类型集合作为设置...

【问题讨论】:

    标签: visual-studio-2008 application-settings


    【解决方案1】:

    我希望您尝试一些事情。

    1. 确保将设置范围创建为 USER 。

    http://msdn.microsoft.com/en-us/library/aa730869(v=vs.80).aspx 在设置中添加 2 个字符串(DictionaryKey 和 Dictionaryvalue)并将范围设置为用户,值设置为空白

    1. 设置不包含添加 Dictionary 的选项。所以我建议试试这段代码

    进行 2 次设置并执行此操作

    Properties.Settings.Default.Dictionarykey = "";// To empty previous settings 
    Properties.Settings.Default.Dictionaryvalue = "";// To empty previous settings 
    for (int i = 0; i < yourdictionarycount; i++ )
    {
    Properties.Settings.Default.Dictionarykey += i + "#";  // or any other value                
    Properties.Settings.Default.Dictionaryvalue += "test" + "#";  // or any other value                
    }
    Properties.Settings.Default.Save();
    

    当你检索值时

    使用此代码:

    public Dictionary<int, string> savedsettings = new Dictionary<int, string>();
    
    string[] eachkey = Properties.Settings.Default.Dictionarykey.Split('#');
    string[] value = Properties.Settings.Default.Dictionaryvalue.Split('#');
    
    for (int j = 0; j < eachkey.Length; j++)
    {
      savedsettings.Add(eachkey[i], eachvalue[i]);
    }
    //just to check if the values are being retrieved properly 
    Messagebox.Show(Properties.Settings.Default.Dictionarykey);
    

    问候,

    维韦克桑帕拉

    【讨论】:

    • 是的,它是用户。 “设置不包含添加字典的选项”-但我能够通过浏览到 System.Collections.Specialized 命名空间来添加 ListDictionary?尽管如此,您添加两个字符串的想法(我想我会使用 StringCollections 的方法)会成功。谢谢!
    • 没问题拉拉,祝你有美好的一天:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-24
    • 1970-01-01
    • 2011-01-20
    • 1970-01-01
    • 1970-01-01
    • 2010-09-24
    • 1970-01-01
    相关资源
    最近更新 更多