Never underestimate the power of Passion!

Thursday 9 February 2017

On 22:43 by Vardan Kumar in    No comments
Saving and Retrieving

Save and retrieve credentials in C# Windows form application

While developing a windows form application which accepts some details from the user, you might want to provide user a feature to remember those details and if user chooses to remember his details then the next time he will run the application, credentials will be auto-populated for quick log in.

Now firstly lets see how to save details in a file....

In any application the main source of details from user is a text box, hence in here also we ll use text box as the main source of information from the user. Also we might want to provide user an option if he wants to save his details or not. So for this purpose we'll use a check box control. Also for the sake of convenience we'll save each credential in separate line so that retrieving the becomes easy.

Also we'll use ".dat" file extension to save the file. We could use ".txt" also but using .dat is good for security purposes. Also user would not try to modify it but .txt is an extension common to all so a user may try to modify it.

The file should be saved at current directory that is the directory where the user will install our application, hence we won't be specifying any path while saving or retrieving the file.

Also for efficiency of your application we should save the user credentials only after the current thread is executed properly as there might arise a case when user may have entered wrong detail. We might not want to save erroneous details. So Before saving his details we should verify and validate the user data first, if its not correct then an error should be displayed and user should be offered a chance to enter his credentials again till the time he enters correct credentials.

Also we should clear the contents of the file every time a user checks the checkbox, just to mantain correctness and consistency of the file.

Source Code for saving the credentials in the file

if (chkBx_Remember.Checked)  //check if check box is checked
 // check if file exist in current directory
                    if (File.Exists("winform.dat"))    
                        System.IO.File.WriteAllText(@"winform.dat", string.Empty); // empty contents of file
//Array list to save each credential at different index
                    List<String> save = new List<String>();
//write data in the file line by line if it exist or create a new file if file doesn't exist
                    using (TextWriter tw = new StreamWriter("winform.dat"))
                        foreach (String s in save)
                            tw.WriteLine(s); //Each credential will be saved in separate line


Saving and retrieving details in/from file C# Windows Form Application
Saving details in file

Now lets see how to retrieve data from file and populate the respective text box

Since we have stored the details in the file we should also retrieve them, off course that is the reason we saved the details in the file.

Also we should include the retrieving logic while loading the thread on which we want to populate the details off course. Generally for an application a login thread is the foremost so its better to include this logic in Form1_Load method(default).

We should first check if the file exists, because file will not be present if user has not saved any details or he might be logging in for the first time what so ever the reason may be.

Source Code for retrieving details from the file

 private void Form1_Load(object sender, EventArgs e)
//Check if the file exists in the current directory
            if (File.Exists("winform.dat"))
           // Array list to retrieve each credential at different index
                List<string> f_details = new List<string>() { };
                String line;
                System.IO.StreamReader file = new System.IO.StreamReader(@"winform.dat");
                while ((line = file.ReadLine()) != null)
// Populating the credentials in their respective text box
                txt_UserName.Text = f_details[0];
                txt_Password.Text = f_details[1];
                txt_Password.UseSystemPasswordChar = true;
                txt_Host.Text = f_details[2];
                txt_PortNo.Text = f_details[3];
                txt_ServiceName.Text = f_details[4];



Post a Comment