Search code examples
c#classpersistencesettings

C# what is alternative for settings


I will like to save settings which persist over multiple runs, basically same thing as settings in C# does, when using Settings.Default.Save().

Is there some alternative to store variables in class or something similar?


Solution

  • You have a lot of options here.

    • Save settings to windows registry.
    • Save settings to a text file.
    • Save settings in a database.

    Here's how I generally go about saving application settings:

    • Create a Settings class with settings as properties that I want to save.
    • Create functions like LoadSettings() and SaveSettings().
    • SaveSettings is responsible for creating an instance of the settings, then serialize it and save it. I use Newtonsoft.JSON to serialize into JSON.
    • LoadSettings reads a file with saved data, deserialize it, and populate the UI with it.

    If you implement something similar, you can even save the settings file with a custom file extension and then associate that extension to your application.

    This way, in your main method, you can check which settings file to load.

    I generally use this approach when I have to allow more than one instance of my application because the <namespace>.Properties.Settings doesn't cut it in that case.