【问题标题】:Windows Forms Settings Values Don't Persist Between SessionsWindows 窗体设置值不会在会话之间保留
【发布时间】:2015-04-11 23:17:40
【问题描述】:

我正在开发一个 Windows 窗体应用程序,理想情况下它将在会话之间存储库存值。

过去,我能够在会话之间保存 Properties.Settings 值。但是,在我当前的应用程序中使用类似的代码我无法这样做。

这是我尝试保存值的代码的 sn-p:

    foreach (KeyValuePair<string, double> ingredient in inventoryDictionary)
    {
        var ingredientUnit = unitList[ingredient.Key];
        lstInventory.Items.Add(item: string.Format(@"{0} {1} {2}", ingredient.Key, ingredient.Value, ingredientUnit));
        settings[ingredient.Key] = ingredient.Value;
        settings.Save();
    }

值在活动会话期间存储,但一旦表单关闭,值总是重置为默认值。

我的所有设置都列在“用户”范围下,并且按预期具有双倍的“类型”。此外,设置中的所有值都默认设置为 0。

在此问题上的任何帮助将不胜感激。我之前能够在会话之间保存值,但我不确定为什么此时它不起作用...

谢谢

【问题讨论】:

    标签: c# winforms visual-studio session persistence


    【解决方案1】:

    试试 Properties.Settings.Default

    例子:

    Properties.Settings.Default.myColor = Color.AliceBlue;
    

    如果你想在会话之间保持它

    Properties.Settings.Default.Save();
    

    The MSDN link here

    【讨论】:

    • 谢谢 alainlompo,您的方法按预期工作。作为参考,我在上面将设置定义为“Properties.Settings.Default”。我试图在初始表单加载时将所有设置值转换为字典。但是,此表达式无法正常工作,因为它返回的所有值都是“0”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 2014-01-20
    • 2015-08-06
    • 1970-01-01
    • 2015-10-21
    相关资源
    最近更新 更多