Properties.Settings.Default.Save() 不保存

Properties.Settings.Default.Save() does not save

[找到了几篇相关文章,但 none 有一个解决方案为我解决了这个问题。]

我正在尝试在 运行 程序的会话之间存储用户选择的选项。所有使用 Properties.Settings.Default.Save() 的尝试都没有影响;更改设置值不会在已安装程序的会话之间持续存在。 SettingsSave() 方法没有捕获异常。

private void SettingsLoad()
{
    SettingsLoad(this);
}

private void SettingsSave()
{
    try
    {
        SettingsSave(this);
        MessageBox.Show("Values written successfully", "Saved");
    }
    catch (Exception ex)
    {
       MessageBox.Show(Utility.ParseException(ex), "EXCEPTION!");
    }
}
....

public static void SettingsLoad(object main)
{
    foreach (SettingsProperty setting in Properties.Settings.Default.Properties)
    {
        // Attempt to get a reference to the property and, if found, set its value
        var prop = main.GetType().GetProperty(setting.Name);
        if (prop != null)
        {
            prop.SetValue(main, setting.DefaultValue);
        }
    }
}

public static void SettingsSave(object main)
{
    foreach (SettingsProperty setting in Properties.Settings.Default.Properties)
    {
        // Attempt to get a reference to the property and, if found, update its value
        var prop = main.GetType().GetProperty(setting.Name);
        if (prop != null)
        {
            setting.DefaultValue = prop.GetValue(main).ToString();
        }
    }
    // This line has no impact; changes are not persisted
    Properties.Settings.Default.Save();
}

我觉得你的代码有点混乱。

阅读属性:

string val = Properties.Settings.Default.Property_Name;

要写一个属性:

Properties.Settings.Default.Property_Name = val;

要保存所有属性:

Properties.Settings.Default.Save

属性 值将在您下次 运行 程序时恢复。