Thursday, May 27, 2010

Saving User Preferences in C#.net Application

Preview:
In some applications we want to store the user preferences, like size and location, of the .net application. Not even store the user preference we required to give some option to user to change his preferences.

Where to store:
There are many options to achieve this. Either we can use XML file to store these settings in and can read these settings while launching of the application or we can use settings file that is inbuilt XML file to store user settings.

XML is a better option if we want control those options at the customer end without launch of the application. Also XML can be stored on any custom path either opted by developer or opted by user to store things.

Settings file is can't be customized, once it is deployed on user's machine, neither by user nor by developer. It is also an XML file but inbuilt in project. Settings store in this file support a vast range of data types. We are using here string and Boolean type only.

Settings File:
Settings file is there in the project itself. You can open the settings file either from the Properties/Settings.settings or you can open it from project properties and settings tab. It has a very friendly UI. We can add the settings easily into this settings file. Figure 1.1 shows the settings file from the Properties folder and Fig 1.2 shows the settings file opened from project>>properties>>settings.













Fig 1.1 Properties/Settings.settings














Fig 1.2  project properties and settings tab


How to load settings:
Here is the method that can be written while loading of the application to set the location ans the start position of it. This is a well documented and self explanatory method. We have handled the FirstLaunch of the application using an Boolean variable "FirstLaunch ". If application is launched first time then set thelocation to center of screen.



















Fig 1.3  Load settings

How to update settings: 
When user has finished the changed in the application or when he is closing the application we can update the settings using following method. Saving all the settings (Size and Location here) and set the FirstLaunch to false.















Conclusion:
Here we have seen that it is very easy to use the Settings file in the .net application. How we can accommodate the settings file in the project for reading and for updating the user preferences.

1 comment:

  1. The only drawback of this approach is that on upgrading the product/application user settings will be overwritten.

    ReplyDelete