0

I have some application level settings for a windows form, which I can read using Properties.Settings.Default.offset. I wanted to persist these settings, so that these settings can be used later.

As per this post at SO, I tried the following-

Properties.Settings.Default["offset"] = this.offsetTextBox.Text;
Properties.Settings.Default.Save();

But it is not saving the value, since I can see the old value even after changing it.

Community
  • 1
  • 1
ravi
  • 6,140
  • 18
  • 77
  • 154
  • 1
    Please, do not include information about a language used in a question title unless it wouldn't make sense without it. Tags serve this purpose. – Ondrej Janacek Mar 24 '14 at 17:28
  • possible duplicate of [Why are my application settings not getting persisted?](http://stackoverflow.com/questions/1054422/why-are-my-application-settings-not-getting-persisted) – Nicholas Carey Mar 24 '14 at 17:57

1 Answers1

0

Per the documentation, inspection of the generated code and the question, "Why are my application settings not getting persisted?", application-scoped settings are read-only.

To update application-scoped settings, you'll need to change the appropriate <appsettings> value in your app.config file:

Note that a change to an app.config file is supposed to restart the app domain, so trying to update your own app.config may behave a little differently than what you might expect.

Also note that the changes will be made to the generated *.exe.config file in your bin directory, not to the app.config file in your Visual Studio solution.

Another thing to consider: in a deployed application, unless you run with "elevated privileges", you're also unlikely to have write access to the app.config file.

If you want to persist/update this sort of app settings, take a page from the OS X book and create a *.plist:

  1. On application start, check for an XML file for your settings in a known location to which that app has read/write access. In OS X, the app-level settings live in /Library/Preferences; user level preferences live in ~/Library/Preferences.

    If it doesn't exist, create it, populating it with the default values for the settings in question (which could be as simple as copying your <appsettings> element from app.config.

  2. Read your app-level settings from that file.

  3. Place a FileSystemWatcher on that file to hook any changes and update the settings.

There you go. Now, you can update your app-level settings to your heart's content. If you ever need to revert, all you have to do is blow away the *.plist file.

Community
  • 1
  • 1
Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135