用户设置无法加载/保存

时间:2015-08-21 13:57:06

标签: c# settings application-settings

我在加载/保存应用程序设置时遇到此问题。我正在保存这样的设置

    string value = "test";
    Properties.Settings.Default.test1 = value;
    Properties.Settings.Default.Save();

如果我在程序仍在运行时加载设置,但是每当我关闭应用程序并再次启动它时,保存的值就会消失。

    string savedValue = Properties.Settings.Default.test1;

我在保存后尝试过刷新,但这似乎没有帮助。

但是我发现了存储设置的位置(Appdata \ Local),并且值确实显示在那里,但它没有被加载。

2 个答案:

答案 0 :(得分:0)

这就是它的完成方式。如果您的代码不起作用,请尝试创建MCVE以启用进一步调试。

namespace ConsoleApplication5
{
    using System;

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("This should be blank on first run, but not on subsequent: {0}", Properties.Settings.Default.test1);

            string value = "test";

            Properties.Settings.Default.test1 = value;

            Properties.Settings.Default.Save();
        }
    }
}

传递#1:

This should be blank on first run, but not on subsequent:
Press any key to continue . . .

传递#2:

This should be blank on first run, but not on subsequent: test
Press any key to continue . . .

答案 1 :(得分:0)

我是WPF开发人员,所以我确信使用

时,Properties.Settings肯定会保存
Properties.Settings.Default.Save();

或者

Properties.Settings settings= new Properties.Settings();
settings.YourParameter=true;
settings.Save();

如果您未在设置中获取更新值,则首先使用 settings = new Properties.Settings(); 你一定会得到新的结果。

相关问题