开发者

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

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 开发者_StackOverflow中文版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.


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


an integer value called RunNumber with Scope User

User scope means that the actual values is not saved in App.config but under a Users\<user>\AppData\... folder.

If your console app runs under an Admin account (or is not placed in Program Files) you can use a Application scope setting.


Since the values for a user scoped setting are stored in a user file and not in app.config, maybe the best approach would be to add the ability to call your app with a specific switch to reset the value you want. e.g. myapp.exe /setcount=123.

Of course, you would have to run the app as the appropriate user to make sure you set the value in the correct file.


Simply use Reload() before accessing your Settings class. You could modify the generated Settings class to call Reload each time the specific property is accessed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜