Saving User and Application Settings in WinForms

January 22, 2009 | By

Before the .NET Framework application settings were saved in INI files and the Windows Registry. But the .NET Framework introduced a much easier way using an XML file. The file has the name of the Assembly and a .exe.config extension, and is placed inside the application’s folder. This way is cleaner because when an application is uninstalled or deleted the end user does not have to worry about left over registry keys or INI files in the Windows directory.

Each setting has four properties: name, type, value, and scope. The type can be a data type or an object. The scope can be user or application. The main difference between them is settings with application scope are read only at run time, while settings with user scope are read/write at run time.

Creating a New Settings

To start creating new settings, from the Solution Explorer right-click on the project node and select Properties, or expand the Properties node and double-click the .setting file. And then enter the name, type, scope and value for each setting.

Project Settings

 

Manipulating the Settings

To read and save the value of a setting in C# use the Properties object. The following example uses a TextBox and 2 Buttons named newValueTextBox, displayButton and saveButton respectively.

Filed in: WinForms | Tags: ,

Comments (9)

  1. Nisse

    Hello

    When I use this row
    Properties.Settings.Default.SQLFormsTestConnectionString = tbConnectionString.Text;

    I get compiler error.
    Inventory.Properties.Settings.SQLFormsTestConnectionString’ cannot be assigned to — it is read only

    So… what is your solution to this and it seams that there is none.

  2. Hello Nisse,
    Could you give me some more details like, did you create SQLFormsTestConnectionString using the Settings tab and what is the scope?

  3. me too

    I get cannot be assigned to — it is read only trying to assign to a setting. this doesn’t work. application scope.

  4. It seems like both of you are getting the same error, you are trying to assign a value to a setting that has an application scope. Settings with application scope are meant for things like connection strings, which the user cannot change at run time.

    So if that’s the case just delete the setting and create a new one with the same name and change the scope to User. That should solve it.

  5. I am not very happy that people keep saying things like Change Application Settings, when in fact they are not showing how to change application settings.. They are showing how to change User Settings at run time. I did not find this helpful at all.

  6. Ch. adeel

    I am declaring different Names keeping their scope user type bool and default value to false. When a user logs in, i get all the permissions assigned to him and update settings accordingly so that i could access them in my whole project and i don’t have to bring them from my database again and again and don’t have to store them somewhere else as well.

    Please let me know if its the correct idea or if you have better idea than this.

  7. Deepak

    Ver nice article… Thanks for giving this……

  8. Sam

    It doesn’t work if you put your application in windows startup. I always get the variables null. But if i start the application by the icon it works well. Don’t know how to fix this…

  9. Just did it.
    Thanks!