Visual Studio 2008 应用程序设置未保存?

发布于 2024-11-11 20:08:03 字数 516 浏览 7 评论 0原文

我知道:

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

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

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

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

I know:

Application settings can be stored as any data type that is XML serializable or has a TypeConverter that implements ToString/FromString. The most common types are String, Integer, and Boolean, but you can also store values as Color, Object, or as a connection string.

I have a ListDictionary class setting - which is serializable but everytime I start up my app again it is null despite creating, assigning and saving a ListDictionary to my settings:

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

How can I use such a class as an application setting? I need a dictionary type collection as a setting...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

知你几分 2024-11-18 20:08:03

我希望你尝试一些事情。

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

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

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

问候,

Vivek Sampara

There are a set of things i would like you to try.

  1. make sure you create the settings scope as USER .

http://msdn.microsoft.com/en-us/library/aa730869(v=vs.80).aspx
add 2 strings(DictionaryKey and Dictionaryvalue) to the settings and set the scope as user and value as blank

  1. Settings doesn't contain an option to add Dictionary . So what i would suggest is try this code

make 2 sting settings and do this

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();

And when you are retrieving the values

use this code:

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

Regards,

Vivek Sampara

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文