Application Settings for Windows Forms .NET 2.0
Application Settings in .NET 2.0 allows to a developer to concentrate on real tasks, and not re-implement settings handling for every new application.
I did not have any experience with Application Settings, but with help of Visual Studio (it automatically generates all required stuff), it took five minute to add settings to an application.
The only problem was to get settings from previous version(s) of application. So it was time to load MSDN and read the documentation :)
ApplicationSettingsBase class has almost everything for the task: migration should be performed right after the settings was loaded, so it can be performed in handler of SettingsLoaded event. And Upgrade method allows to automatically migrate settings.
So all you need to do is to add a special boolean setting IsFirstRun with default value as true, and in SettingsLoaded handler call Upgrade if IsFirstRun is false.
Here is a snippet from Settings.cs (it should be generated by Visual Studio):
internal sealed partial class Settings
{
public Settings()
{
SettingsLoaded += new SettingsLoadedEventHandler(Settings_SettingsLoaded);
}
void Settings_SettingsLoaded(object sender, SettingsLoadedEventArgs e)
{
if (IsFirstRun)
{
Upgrade();
IsFirstRun = false;
}
}
}
0 Comments:
Post a Comment
<< Home