Kiran
Kiran

Reputation: 8518

Settings.Settings is updated to default values in C#

I have the settings file in C# where I store the configuration parameters. So, I build the solution and install it in the system. The application creates an XML file user.config consisting of all the configuration parameters in :

%userprofile%\appdata\local or %userprofile%\Local Settings\

I made changes to the configuration through the application and save it by issuing the command

Properties.Settings.Default.Save();

When I restart the application, the configuration consists of the default values and not the updated values.

Any idea if I am missing something here.

enter image description here

Upvotes: 0

Views: 158

Answers (2)

Hans Passant
Hans Passant

Reputation: 941208

%userprofile%\appdata\local or %userprofile%\Local Settings\

This is what is fundamentally wrong about the question, this is not where the user.config file is stored. The LocalFileSettingsProvider class (the default settings provider class) stores the user.config file in a directory with an unspeakable name, like C:\Users\username\AppData\Local\WindowsFormsApplication1\WindowsFormsApplication1._Url_twchbbo4atpsvjpauzkgkvesu5bh2aul\1.0.0.0

The twchbbo4atpsvjpauzkgkvesu5bh2aul part of the name is produced by a hashing function that combines the program install location, program name, [AssemblyCompany] and [AssemblyProduct] to ensure that the directory is unique and will not clash with the user.config of another program. And appending the [AssemblyVersion] so that different versions of the program won't clash.

You'll need to diagnose this problem by first finding the right user.config file back. Start from the %appdata%\Local or %appdata%\Roaming directory. Ensure that the Save() method actually saves, it won't if no setting was assigned a different value. Use SysInternals' ProcMon utility to double-check all assumptions, you'll see your program accessing the file in the trace.

Upvotes: 2

Ventsyslav Raikov
Ventsyslav Raikov

Reputation: 7192

What is the Scope of your settings? You cannot change Application Scoped settings like that from code. Take a look at the settings designer. enter image description here

You can read more about settings scope here.

Upvotes: 1

Related Questions