How to change application settings (Settings) while app is open?
Asked Answered
J

4

8

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?

Jonquil answered 23/7, 2009 at 10:19 Comment(1)
That should be saving, I don't see any problems with it in my eyes.Ikey
U
5

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.

Unexacting answered 23/7, 2009 at 10:49 Comment(0)
A
11

settings scope must be user not application

Antilogism answered 23/7, 2009 at 10:39 Comment(1)
Add before your setting in Settings.designer.cs [global::System.Configuration.UserScopedSettingAttribute()] or simply change the scope in settings1.settingsAntilogism
U
5

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.

Unexacting answered 23/7, 2009 at 10:49 Comment(0)
I
2

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.

Impute answered 23/7, 2009 at 10:44 Comment(1)
whichever file it is updating, as a result I would like to see the value of ComplexValidationsString to be set to the value set in the previous time the app was opened.Jonquil
L
0

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

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.

Lauretta answered 20/10, 2009 at 1:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.