views:

301

answers:

2

I need to give the user the ability to change application settings, in this case the location for the application database. I noticed that Application Settings are read only at run time, but this needs to be application-wide, not user-specific. How do I persist an application-wide connection string in windows.forms that is changeable at runtime?

+2  A: 

You can use

ConfigurationManager.AppSettings.Set()

Also

ConnectionStringSettings.ConnectionString Property

Can be set too.

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Add("test","tada");
config.Save(ConfigurationSaveMode.Minimal, true);
astander
-1: Only for user-level stuff, and you dont want to write to Progrm Files directory
Ruben Bartelink
-1 removed in light of edit - but if you are going this route, you should definitely not be writing beside the EXE or in the EXE's .config file - you should be writing to the AppSettings directory
Ruben Bartelink
A: 

There isnt a direct way in the framework using the newer Settings classes - you can save user-level settings with the Settings support. Easiest way is to XmlSerialize a class and store it in the AppSettings directory (the program directory shouldnt be written to)

EDIT: Yes, the old appSettings mechanism has a way of writing, but that's bad news - the newer Settings stuff omits this facility as it is bad news for lots of reasons to try to write to config files in Program Files

Ruben Bartelink