views:

399

answers:

2

I know it is a good idea to store configuration data in app.config (e.g. database connection strings) instead of hardcoing it, even if I am writing an application just for myself. But is there a way to update the configuration data stored in app.config from the program that is using it?

+5  A: 

If you use the Settings for the project, you can mark each setting as either application or user.

If they're set as user, they will be stored per-user and when you call the Save method it will be updated in the config for that user.

Code project has a really detailed article on saving all types of settings: http://www.codeproject.com/KB/dotnet/user_settings.aspx

Glenn Slaven
+1  A: 

app.config isn't what you want to use for user-tweakable data, as it'll be stored somewhere in Program Files (which the user shouldn't have write permissions to). Instead, settings marked with a UserScopedSettingAttribute will end up in a user-scoped .config file somewhere in %LocalAppData%.

I found the best way to learn this stuff was to mess with the Visual Studio "Settings" tab (on your project's property pages), then look at the code that it generates and look in %LocalAppData% to see the file that it generates.

Domenic