Save Settings in a .NET Winforms Application

47,990

Solution 1

At some point, the answer boils down to a matter of taste. I'd say you'll end up with at least these options:

  • store it in the registry, where you have the HKEY_CURRENT_USER key. Everything under it is user-specific. This is usually preferred when you want to store a bunch of small key-value pairs. The main advantage of this system is that your settings are easy to find and share throughout several different applications. To follow this path, you can start from here.
  • using .NET Application settings, provides the easiest way to access your settings at runtime. Again, it's better for using with key-value pairs of small-sized data. IMO, the main advantages of this method is its simplicity and the fact that it empowers you to use some .NET classes as values (not forcing you to convert everything into more basic types). To follow this path, you can start from here.
  • store it in User Data folders, which are usually hidden under the user's profile directory. This is preferred when you want to store a large amount of data or any number of files. The main advantage of this method is that you can manipulate your data as you would with any files (that may also be a disadvantage). To follow this path, you can start from here.

Solution 2

You can use the settings infrastructure provided by .NET. In your project's property pages, go to the Settings page, and define your settings. You can define the scope of each setting as "Application" or "User". A class will be automatically generated to access these settings from code.

To access settings Foo and Bar, use :

// Read settings
textBoxFoo.Text = Properties.Settings.Default.Foo;

// Write settings
Properties.Settings.Default.Bar = checkBoxBar.IsChecked;

// Save settings
Properties.Settings.Default.Save();

Solution 3

I would use Application Settings. It's pretty straightforward and will take care of some issues for you (such as not having write access to the folder where your app may be installed without administrative access, which rules out directly using app.config for your settings).

Share:
47,990

Related videos on Youtube

Jimbo
Author by

Jimbo

Updated on July 09, 2022

Comments

  • Jimbo
    Jimbo almost 2 years

    Possible Duplicate:
    What is the best way to store user settings for a .NET application?

    I have found alot of VERY different examples as to how one would save application settings (per user) from a Winforms application.

    I imagine that the correct way to do this is very simple in c# and am hoping someone can enlighten me?

    • Aaronaught
      Aaronaught almost 14 years
      Possible duplicate of What is the best way to store user settings for a .NET application? (This has to be one of the most duplicated questions in the .NET tags...)
    • No Em
      No Em over 6 years
      There is another way to save settings in .Net is Config4Net that is an alternative to built-in settings feature of .Net. You can also show the UI of your config to edit at the runtime, it saves your time especially in creating settings form for our app.
  • Jimbo
    Jimbo almost 14 years
    Is there no way of doing this directly from your code? i.e. Do you have to pre-set these up in the projects property pages first?
  • Thomas Levesque
    Thomas Levesque almost 14 years
    yes, settings have to be predefined in the .settings file
  • Johan Larsson
    Johan Larsson almost 11 years
    Application scoped settings will not be saved using this I think.
  • Thomas Levesque
    Thomas Levesque almost 11 years
    @JohanLarsson, indeed they won't. App scoped settings can't be changed at runtime, at least not by the usual means; obviously you could write directly to the .config file, if you have write access to it.
  • Brett
    Brett almost 7 years
    "using .NET Application settings" link is broken now
  • Phi
    Phi over 2 years
    If in the second method, your saved Settings are being lost between each version of your app, make sure you have "embed default manifest" enabled in project properties. No manifest = reset each version.