6

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

Hello. What is the best way to store application settings in .Net 4.0? Windows Registry? Application.Settings?

What if I want to be able to update program and keep settings from older version (New version can have more new setting and some old ones deleted)?

What if I want to store collection of my custom objects?

I know there is similar question but it's about .Net 2.0. Maybe there are some new ways of saving settings in 4.0.

Thank you in advance.

Community
  • 1
  • 1
Hooch
  • 28,817
  • 29
  • 102
  • 161
  • @Scott Anderson I'am asking for .Net 4.0. Link you gave me is about 2.0. – Hooch Mar 21 '11 at 14:54
  • There's nothing new in 4.0. Or 3.0 or 3.5 – Hans Passant Mar 21 '11 at 14:56
  • Nothing changed in .NET 4.0 related to this. The dupe question is still valid and recommended in .NET 4.0. – Scott Arrington Mar 21 '11 at 14:56
  • Take a look at System.Configuration namespace. The application settings its well suited for small values. If you want to store complex objects System.Configuration classes can help you a lot. You can define your own tags to use inside Web.Config or App.Config files. – Arturo Martinez Mar 21 '11 at 14:58

3 Answers3

2

I don't think .NET 4 added anything new with regard to application settings.

See this for what's new in .Net 4. http://msdn.microsoft.com/en-us/library/ms171868.aspx

BenCr
  • 5,991
  • 5
  • 44
  • 68
1

MS has been trying to get people out of the registry for the past 2 OS versions. Also, as @Scott Anderson said in a comment, nothing has significantly changed with .Net 4.0 in this regard.

If this is a local application with no backing data store, then go ahead and use the app.config file.

If you want to store local data then I'd recommend using something like sql lite or a similar mechanism in which you can easily create tables and query it as necessary. This would help with versioning as well.

NotMe
  • 87,343
  • 27
  • 171
  • 245
1

Application.Settings is definitely the way to go - unlike registry settings it will work regardless of Operating System, user level or when running your application from a Terminal Services client.

In the past I have created a property called MigrateUserSettings and used it to help me manage migration of user settings.

I set the value for this property to true in the application by default. On startup, if I find it is ever set to true, I attempt to migrate the settings and then set the value to false (it then remains false until the application is upgraded).

Example:

if (Settings.Default.MigrateUserSettings)
{
    Settings.Default.Upgrade();
    Settings.Default.MigrateUserSettings = false;
    /* Custom handling of migrating specific settings (if required) here
       e.g. if any have been renamed or if the possible options have changed. */
    Settings.Default.Save();
}
Iain Collins
  • 6,774
  • 3
  • 43
  • 43
  • How does MigrateUserSettings get set back to true? That is one part of this tactic that I don't understand because with my tests (with .NET 4.0 and ClickOnce) - publishing a new version of my program doesn't cause that setting to back to it's default value. – Wayne Bloss Nov 26 '13 at 21:30
  • Hmm sorry I can't answer that, it works for me (I *think* even if I just run the new build directly, without 'installing'). Perhaps it's dependant on something like a build/file version being bumped that isn't? – Iain Collins Nov 27 '13 at 06:27
  • In which namespace is `Settings` class ? – Tschareck Mar 18 '16 at 08:50