[c#] Write values in app.config file

I struggled with this for a while and finally figured it out on my own. I didn't find any help at the time, but wanted to share my approach. I have done this several times and used a different method than what is above. Not sure how robust it is, but it has worked for me.

Let's say you have a textbox named "txtName", a button named "btnSave" and you want to save the name so the next time you run your program the name you typed appears in that textbox.

  1. Go to Project>Properties>Settings and create a setting -
  • name = "Name"
  • type = "string"
  • scope = "user"
  • value you can leave blank.

Save your settings file.

  1. Go to your form where textbox and button exist. Double click your button and put this code in;
    //This tells your program to save the value you have to the properties file (app.config);
    //"Name" here is the name you used in your settings file above.
    Properties.Settings.Default.Name = txtName.txt;
    //This tells your program to make these settings permanent, otherwise they are only
    //saved for the current session
    Properties.Settings.Default.Save();
  1. Go to your form_load function and add this in there;
    //This tells your program to load the setting you saved above to the textbox
    txtName.txt = Properties.Settings.Default.Name;
  1. Debug your application and you should see the name you typed in.
  2. Check your application debug directory and you should see a .config file named after your program. Open that with a text editor and you will see your settings.

Notes -

  • "Name" refers to the actual name of the setting you created.
  • Your program will take care of creating the actual XML file, you don't have to worry about it.