为什么没有保存 Properties.Settings.Default?

发布于 2024-08-18 16:11:17 字数 583 浏览 2 评论 0原文

我写这个是为了快速测试

为什么我的设置没有被保存?我第一次运行这个时,我有 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 技术交流群。

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

发布评论

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

评论(1

天煞孤星 2024-08-25 16:11:17

您应该使用公开的属性,而不是将数据放入上下文中:

var saveData = MyApp.Properties.Settings.Default;
saveData.user = textBox1.Text;
saveData.pass = textBox2.Text;

上下文

提供上下文信息
提供者可以在持久化时使用
设置

,据我了解,不用于存储实际设置值。

更新:如果您不想使用 Visual Studio 中的设置编辑器生成强类型属性,您可以自己编写代码。 VS 生成的代码具有如下结构:

    [UserScopedSetting]
    [DebuggerNonUserCode]
    [DefaultSettingValue("")]
    public string SettingName
    {
        get { return ((string)(this["SettingName"])); }
        set { this["SettingName"] = value; }
    }

您可以通过编辑 Settings.Designer.cs 文件轻松添加更多属性。

如果您不想使用强类型属性,可以直接使用 this[name] 索引器。那么你的例子将如下所示:

    var saveData = MyApp.Properties.Settings.Default;
    saveData["user"] = textBox1.Text;
    saveData["pass"] = textBox2.Text;

You should be using the exposed properties instead of putting your data in the context:

var saveData = MyApp.Properties.Settings.Default;
saveData.user = textBox1.Text;
saveData.pass = textBox2.Text;

The context

provides contextual information that
the provider can use when persisting
settings

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:

    [UserScopedSetting]
    [DebuggerNonUserCode]
    [DefaultSettingValue("")]
    public string SettingName
    {
        get { return ((string)(this["SettingName"])); }
        set { this["SettingName"] = value; }
    }

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:

    var saveData = MyApp.Properties.Settings.Default;
    saveData["user"] = textBox1.Text;
    saveData["pass"] = textBox2.Text;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文