Harsh
Harsh

Reputation: 103

User settings in C#

I am trying to save Name Value pair in settings dynamically. I can see the new saved values dynamically. But, when I access the settings page from the property window , it shows the same old value. Here is the code :

 Properties.Settings.Default.Name = textBox1.Text;
 Properties.Settings.Default.Age = textBox2.Text;
 Properties.Settings.Default.Save();

Any Suggestions?

Upvotes: 0

Views: 539

Answers (3)

João Simões
João Simões

Reputation: 1361

Assuming you are testing your application with Visual Studio, your problem happens because, when you are changing the setting of your application you aren't changing the original setting file. When Visual Studio starts running an application, it creates a folder inside the directory where your code is named "obj/Debug" or "obj/Release" and copies all your DLLs and resources into that folders, including settings files.

This means that changes to settings will be reflected in your "obj/Debug/yourappname.exe.config" and not in the original file. If you open this file, for example, with a text editor you'll see the contents have changed. Remember that every time you recompile your application in Visual Studio and start running this file will be replaced by the original, losing your new settings.

You can manually run your .exe application inside that folder and validate if your settings have persisted.

Upvotes: 3

Patrick Allwood
Patrick Allwood

Reputation: 1832

The solution->Properties->Settings is where you set the default values. User settings can be changed at run-time, and will persist with your application, but they are not written back to your visual studio project.

In your example, if you run the program again on subsequent times (* not by hitting debug / rebuild in visual studio), your settings that are saved in your snippet will have been persisted.

Imagine that your program has been deployed on user's pc's - there shouldn't be a mechanism for that to change your visual studio project settings file.

Upvotes: 1

csteinmueller
csteinmueller

Reputation: 2487

After compile the settings file gets deployed into a yourapplication.exe.config file. This is the file you are changing (/Debug/app.exe.config). If you want to see changes in your "property window" you have to manually open the .settings file and edit the xml.

Note: after changing the .config file the changes are persistent .. but only until you compile your application again.

Upvotes: 2

Related Questions