tags:

views:

123

answers:

2

In my application I allow user to choose a file with some data, data is read and it should be available when user closes and opens the application again. I thought that one could store the value in Settings so I wrote a class:

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

        //some more
    }

After setting the value and closing the application the value was lost. Is there any similar way to do it so that the value is preserved? I've once seen some code to replace data written in config file so I suppose there is a way.

+7  A: 

It's as easy as

Properties.Settings.Default.Save();

You can put this in you static void Main() method

ybo
+2  A: 

In addition to ybo's answer, you should be aware that .NET saves the settings file in a folder that relates to both where you launched the application from and what version number it is.

This means that if you update the version number (or run it from a different place), you will get a new blank set of settings. I add a property that lets me detect when we are running a new version for the first time, and try to upgrade from the previous settings. You can retrieve old settings by using.

 Settings.Default.GetPreviousVersion("MyPropertyName");

You do need to remember to catch SettingsPropertyNotFoundException if you have added new properties since an earlier version.

Mark Heath