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?

Martin Brown
  • 24,692
  • 14
  • 77
  • 122
agnieszka
  • 14,897
  • 30
  • 95
  • 113

4 Answers4

11

settings scope must be user not application

Woland
  • 1,050
  • 3
  • 19
  • 36
  • Add before your setting in Settings.designer.cs [global::System.Configuration.UserScopedSettingAttribute()] or simply change the scope in settings1.settings – Woland Jul 23 '09 at 10:56
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.

H H
  • 263,252
  • 30
  • 330
  • 514
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.

Graham Clark
  • 12,886
  • 8
  • 50
  • 82
  • 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 Jul 23 '09 at 10:48
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.

Jader Dias
  • 88,211
  • 155
  • 421
  • 625