1

i've created some Application settings (i.e. not User) settings in my Visual Studio project:

enter image description here

This application will be running from a shared (i.e. read-only, network) location. How do i alter the application settings? e.g.

  • DefaultServer: fvirm001
  • DatabasePassword: QnV0IHNoZSdzIGluIGxvdmUgd2l0aCBzb21lb25lIGVsc2Uu

In the olden days (last Thursday) i would create a MyApp.ini file (in the same folder as MyApp.exe), and read the settings from there.

In the new XML .NET world i might change it to MyApp.xml. But then i remembered that .NET already has an XML file to store application settings. (e.g. the customer might want to manage the set of trace listeners in app.config).

How do i manage the <applicationSettings> in app.config?

enter image description here

Can i simply create an app.config file in the application directory, and .NET will use values as an override?

Microsoft's MSDN page on Managing Application Settings does not mention how to manage application settings.

Community
  • 1
  • 1
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
  • Are you talking about changing them, as the developer (e.g. using a new connection string when you migrate databases)? Or are you talking about reading/writing settings at runtime (you mention "the customer")? – Hank Nov 07 '11 at 20:19
  • i mean as "the customer", e.g. customer's IT being able to twiddle a value to point to a new server. And the customer being able to alter the database password as they desire. – Ian Boyd Nov 07 '11 at 20:22
  • It sounds like what you really want are the User settings, which expose the values with `set` and `get` properties and a `Save()` method. You don't have to worry about the XML implementation at all. Why are you adamant about using the Application type? – Hank Nov 07 '11 at 20:25
  • IT would have no way to call `.Save()`. – Ian Boyd Nov 07 '11 at 20:31

2 Answers2

1

You can change the values in the Settings designer window you have open. But yes, they're stored in the config file (ProgramExecutable.exe.config).

Application Settings are not designed to be changed by the user, so there's no way to change them at runtime (unlike User settings, which can be reassigned and saved).

Hank
  • 8,289
  • 12
  • 47
  • 57
  • 1
    Oh, it's tricky. If you do not deploy the `ProgramExecutable.exe.config` the settings are still stored somewhere internally. But if you then place the `.config` file in the folder the application will use the settings from there. So the answer is to place an `MyApp.exe.config` file in the folder, and IT can fiddle with the settings in that text file. – Ian Boyd Nov 07 '11 at 20:30
1

Check out the Reload method of the ApplicationSettingsBase class...

Once you update the settings (either manually or from your UI) you'll have to use this method to reload the settings from the config file.

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.

Dean Kuga
  • 11,878
  • 8
  • 54
  • 108
  • Doesn't really apply to me (as i would just tell everyone to close and reopen the software to pick up the new settings). But +1 for mentioning `Reload` - for whatever intended use it serves. – Ian Boyd Nov 07 '11 at 20:32