views:

710

answers:

4

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?

+3  A: 

settings scope must be user not application

Woland
what do you mean?
agnieszka
Add before your setting in Settings.designer.cs [global::System.Configuration.UserScopedSettingAttribute()]or simply change the scope in settings1.settings
Woland
A: 

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.

Graham Clark
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.
agnieszka
+1  A: 

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.

Henk Holterman
A: 

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.

Jader Dias