Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change the value of a Settings.settings value

Tags:

c#

.net

I have a simple console application which runs daily (called by windows task scheduler) and depends on a value that is incremented each time the application runs. In order to persist this value I chose to use the Settings.Settings file.

So I have an integer value called RunNumber with Scope User which is incremented each time the application finishes running:

Properties.Settings.Default.RunNumber++;
Properties.Settings.Default.Save();

Part of the reason I chose this is that the value is also placed into the app.config file:

<setting name="RunNumber" serializeAs="String">
    <value>0</value>
</setting>

Which means that should the RunNumber have to be incremented unexpectedly it can just be changed in the app.config:

<setting name="RunNumber" serializeAs="String">
    <value>10</value>
</setting>

My expectation being that the next time my application goes to read the value of RunNumber it would take the value set in the app.config (in this case 10). This does not happen and instead the next time the application is run it will use the value it last modified in this case the run number would be 1 instead of the 10 I would expect.

My application accesses the value of RunNumber like this:

Properties.Settings.Default.RunNumber

How can I change the value of the RunNumber without having to modify the application? The idea being if it needs to be changed to a value unexpectedly I could just modify the value in the app.config instead of having to change some code and re-deploy the application.

like image 441
Aesir Avatar asked Jul 06 '11 08:07

Aesir


1 Answers

The value that is placed in the app.config is the default used when a new user uses your application and his settings need to be created. After running the application once a user.config will be created to store the user scope settings.

In order for you to change the value manually you need to do it in the corresponding user.config that you'll find somewhere in the user profile directory, because if you change app.config only new users will see the change.

In Windows 7 without roaming profile and with an Windows Forms Application named Example.Settings.CustomClass I'm getting the following folder for the user.config:

C:\Users\[user]\AppData\Local\Microsoft\Example.Settings.CustomCl_Url_3qoqzcgn1lbyw2zx3oz1o3rsw2anyjsn\1.0.0.0
like image 190
João Angelo Avatar answered Sep 19 '22 22:09

João Angelo