Reputation: 4426
this is very strange.
I have created an app.config file with some settings.
Visual Studio creates a class to manage the settings "easily", but, for some reason, when I call
Properties.Settings.Default.UrlImportacion
The default setting is retrieved (the one that was set at design time). When I change the value in app.config file, only the default one is retrieved.
I tried adding the settings in applicationSettings group and in userSettings group. No matter what I do, system always get the default setting.
Is there an explanation to this?
Thanks Jaime
Upvotes: 0
Views: 1287
Reputation: 1375
Application scoped settings are read only and can be changed at design time or by altering the .config
file in between application sessions.
User scoped settings can be written at runtime, but if you change/delete/add a setting you need to call Properties.Settings.Default.Save();
method to save your changes on settings between application sessions; Otherwise the settings may get cleared each time a change is detected.
To check whether your changes are persisted or not, you can find user settings stored here %userprofile%\appdata\local
or %userprofile%\Local Settings\Application Data
.
Update
If I understand you well, this solution will work for you.
I tried to simulate your situation by copying content of bin folder to new project and changing ProjectName.exe.config
and App.config
file manually, the default value (in old project) for Color
was Green
but my expected value was Purple
.
This section is what I've added to both ProjectName.exe.config
and App.config
files:
<userSettings>
<Namespace.Properties.Settings>
<setting name="Color" serializeAs="String">
<value>Purple</value>
</setting>
</Namespace.Properties.Settings>
</userSettings>
After doing the above output of Properties.Settings.Default.Color
was Green
.
But why this is happening? Because default values are cached, and if the franework is not able to access or open the config
file it will use the defaults instead.
Obviously the problem has a solution. You can solve it simply by calling Reload
method before trying to read the value:
Properties.Settings.Default.Reload();
var color = Properties.Settings.Default.Color;
As described in its documentation:
The Reload method clears the currently cached property values, causing a reload of these values from persistent storage when they are subsequently accessed. This method performs the following actions:
It clears the currently cached properties by clearing the collection represented by the PropertyValues property.
It raises the PropertyChanged event for every member of the Properties collection.
Reload contrasts with Reset in that the former will load the last set of saved application settings values, whereas the latter will load the saved default values.
Upvotes: 2