应用程序打开时如何更改应用程序设置(设置)?

时间:2022-11-05 22:46:05

I've written a class that should allow me to easily read and write values in app settings:

我编写了一个类,可以让我在应用程序设置中轻松读取和写入值:

public static class SettingsManager
    {
        public static string ComplexValidationsString
        {
            get { return (string)Properties.Settings.Default["ComplexValidations"]; }
            set
            {
                Properties.Settings.Default["ComplexValidations"] = value;
                Properties.Settings.Default.Save();
            }
        }

the problem is the value isn't really saved, I mean it is not changed when I exit the application and run it again. What can I do to ensure that the saved value persists between closing and opening again?

问题是值没有真正保存,我的意思是当我退出应用程序并再次运行它时它没有改变。我该怎么做才能确保在关闭和再次打开之间保存的值仍然存在?

4 个解决方案

#1


You should check

你应该检查一下

Properties.Settings.Default.Properties["ComplexValidations"].IsReadOnly

It is probably true, this is what Roland means with "Application Scope". Save will fail silently. Take a look at Project|Properties|Settings, 3rd column.

这可能是真的,这就是Roland对“应用范围”的意思。保存将无声地失败。看一下Project | Properties | Settings,3rd column。

#2


settings scope must be user not application

设置范围必须是用户而非应用程序

#3


Are you sure it's not saving the changes? The [ProgramName].exe.config file in the bin folder won't be updated. The acutal file used is usually put in C:\Documents and Settings\[user]\Local Settings\Application Data\[company name]\[application].exe[hash string]\[version]\user.config. I know when I tried this kind of thing it took me a while to realise this was the file that was getting updated.

你确定它没有保存更改吗? bin文件夹中的[ProgramName] .exe.config文件将不会更新。使用的acutal文件通常放在C:\ Documents and Settings \ [user] \ Local Settings \ Application Data \ [company name] \ [application] .exe [hash string] \ [version] \ user.config中。我知道当我尝试这种事情时,我花了一段时间才意识到这是正在更新的文件。

#4


I just tested a User Setting and it is persisted if you run this Console app twice:

我刚刚测试了一个用户设置,如果你运行这个Console应用程序两次,它会被持久化:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(Settings1.Default.Setting);
        Console.ReadLine();
        Settings1.Default.Setting = "A value different from app.config's";
        Settings1.Default.Save();
    }
}

Just try it out. It won't take a minute.

试一试吧。这不会花一分钟。

#1


You should check

你应该检查一下

Properties.Settings.Default.Properties["ComplexValidations"].IsReadOnly

It is probably true, this is what Roland means with "Application Scope". Save will fail silently. Take a look at Project|Properties|Settings, 3rd column.

这可能是真的,这就是Roland对“应用范围”的意思。保存将无声地失败。看一下Project | Properties | Settings,3rd column。

#2


settings scope must be user not application

设置范围必须是用户而非应用程序

#3


Are you sure it's not saving the changes? The [ProgramName].exe.config file in the bin folder won't be updated. The acutal file used is usually put in C:\Documents and Settings\[user]\Local Settings\Application Data\[company name]\[application].exe[hash string]\[version]\user.config. I know when I tried this kind of thing it took me a while to realise this was the file that was getting updated.

你确定它没有保存更改吗? bin文件夹中的[ProgramName] .exe.config文件将不会更新。使用的acutal文件通常放在C:\ Documents and Settings \ [user] \ Local Settings \ Application Data \ [company name] \ [application] .exe [hash string] \ [version] \ user.config中。我知道当我尝试这种事情时,我花了一段时间才意识到这是正在更新的文件。

#4


I just tested a User Setting and it is persisted if you run this Console app twice:

我刚刚测试了一个用户设置,如果你运行这个Console应用程序两次,它会被持久化:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(Settings1.Default.Setting);
        Console.ReadLine();
        Settings1.Default.Setting = "A value different from app.config's";
        Settings1.Default.Save();
    }
}

Just try it out. It won't take a minute.

试一试吧。这不会花一分钟。