Click here to Skip to main content
12,998,892 members (70,903 online)
Click here to Skip to main content
Add your own
alternative version


14 bookmarked
Posted 19 Mar 2012

Saving User Settings in Winform Application

, 19 Mar 2012
Rate this:
Please Sign up or sign in to vote.
The article displays how to save user setting or preference at run time.


This articles shows how to save the user preference using Settings.setting file so that it can persist between different application sessions in Winform application.


We can store these information in registry , but very few of us know about Settings.setting file which help in doing exactly this.

Using the code  

Consider this tool which asks user to enter Database connection information and provides some output based on input given by ther user from the database.

The problem here is whenever user closes the application and opens it he need to provide the same connection information again. He would definately like to have this information to be remembered so that next time he opens it he doesn't need to input it again, similar to web forms where these information are stored in user cookie.



In big application with proper user log in , these information can be stored in user preference table but for standalone tool like above its best to have it stored in the exe itself without using any file or database.

To add Settings file , select "Add new File" and go to general and add Settings.setting file like below.


Open the Settings.setting designer and add the setting properties name like below."Name" represents the Configurable Property name , "Type" represent type of property, "Scope" represent whether its scope is "Application" or "User". Application Scope makes the the corresponding Setting property as read only.


Once the configurable properties are added and saved in designer it can be used like shown in below code.

namespace Settings
    public partial class UserSettingsDemo : Form
        string ConnectionString = string.Empty;
        public UserSettingsDemo()


        private void button1_Click(object sender, EventArgs e)
            txtOuput.Text = "Ouput goes here...................";

        private void chkIntegratedSecurity_CheckedChanged(object sender, EventArgs e)
            if (chkIntegratedSecurity.Checked)
                txtUserId.Enabled = false;
                txtPassword.Enabled = false;

        private void btnTestConnection_Click(object sender, EventArgs e)
            txtInput.Enabled = true;

        private bool TestConnection()
            return true;

        private void UserSettingsDemo_Load(object sender, EventArgs e)
            txtServer.Text = Settings.Default.ServerNameSetting;
            txtDatabase.Text = Settings.Default.DBNameSetting;
            txtPassword.Text = Settings.Default.PasswordSetting;
            txtUserId.Text = Settings.Default.UserIdSetting;
        private void UserSettingsDemo_FormClosing(object sender, FormClosingEventArgs e)
            Settings.Default.DBNameSetting = txtDatabase.Text;
            Settings.Default.UserIdSetting = txtUserId.Text;
            Settings.Default.PasswordSetting = txtPassword.Text;
            Settings.Default.ServerNameSetting = txtServer.Text;




On Form_Closing event the settings are assigned and saved from the corresponding textboxes.

On Form_Load these values are retrieved and assigned to the corresponding textboxes thus persisting the user information.

Points of Interest

You can also save the size of the form , color or any other user preferences using the similar approach. Persisiting the value using above method is very easy and very handy in making your application user friendly.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer NCS,Singapore
Singapore Singapore
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
gameengineer16-Dec-14 9:20
membergameengineer16-Dec-14 9:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170622.1 | Last Updated 19 Mar 2012
Article Copyright 2012 by Rishikesh_Singh
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid