为什么没有保存 Properties.Settings.Default?
我写这个是为了快速测试
为什么我的设置没有被保存?我第一次运行这个时,我有 3(旧)/3(当前)元素。第二次我得到 3(旧)/5(当前),第三次得到 5(旧)/5(当前)。
当我关闭应用程序时,设置完全消失。当我运行它时又是 3。我没有对应用程序进行任何更改。为什么我的设置没有被保存
private void button2_Click(object sender, EventArgs e)
{
MyApp.Properties.Settings.Default.Reload();
var saveDataold = MyApp.Properties.Settings.Default.Context;
var saveData = MyApp.Properties.Settings.Default.Context;
saveData["user"] = textBox1.Text;
saveData["pass"] = textBox2.Text;
MyApp.Properties.Settings.Default.Save();
}
I wrote this to quickly test
Why arent my settings being saved? The first time i run this i have 3(old)/3(current) elements. The second time i get 3(old)/5(current), third time 5(old)/5(current).
When i close the app the settings completely disappear. Its 3 again when i run it. I made no changes to the app. Why arent my settings being saved
private void button2_Click(object sender, EventArgs e)
{
MyApp.Properties.Settings.Default.Reload();
var saveDataold = MyApp.Properties.Settings.Default.Context;
var saveData = MyApp.Properties.Settings.Default.Context;
saveData["user"] = textBox1.Text;
saveData["pass"] = textBox2.Text;
MyApp.Properties.Settings.Default.Save();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用公开的属性,而不是将数据放入上下文中:
上下文
,据我了解,不用于存储实际设置值。
更新:如果您不想使用 Visual Studio 中的设置编辑器生成强类型属性,您可以自己编写代码。 VS 生成的代码具有如下结构:
您可以通过编辑 Settings.Designer.cs 文件轻松添加更多属性。
如果您不想使用强类型属性,可以直接使用
this[name]
索引器。那么你的例子将如下所示:You should be using the exposed properties instead of putting your data in the context:
The context
and is in my understanding not used to store the actual setting values.
Update: if you don't want to use the Settings editor in Visual Studio to generate the strong-typed properties you can code it yourself. The code generated by VS have a structure like this:
You can easily add more properties by editing the Settings.Designer.cs file.
If you don't want to use the strong-typed properties you can use the
this[name]
indexer directly. Then your example will look like this: