.NET - Best way to store application settings [duplicate]
Possible Duplicate:
What is the best way to store user settings for a .NET application?开发者_StackOverflow
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.
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
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.
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();
}
精彩评论