I have a class library that is going to be called from multiple separate executable applications. This class library needs to know about an address of a database server (and many other configuration options, auth info, etc) to access. I have a configuration and administration application, separate from the class library, that also needs to know and set these configuration options.
Is it be common practice to store these user specific configuration options in the Windows registry, or is it preferred to use the typical 'App.config' XML approach for the class library and allow the configuration tool to change and modify it?
I am leaning toward the registry approach, but I know many people have opinions about not using it. What would you do?
The best practice is to use XML configuration files in the user's %appdata% directory.
There are a number of reasons for this:
Your user should always have access to their own %appdata% directory. Here's a sample:
string configFilePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "myAppConfig.config";
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = configFilePath;
Configuration cfg = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
cfg.AppSettings.Settings.Add("mySetting", "myValue!");
cfg.Save(ConfigurationSaveMode.Modified);
// to read the setting back
string mySetting = cfg.AppSettings.Settings["mySetting"].Value;
// at this point, mySetting = "myValue!"
Remember to add the System.Configuration v2.0.0.0 reference to your project! The default System.Configuration namespace does not have all of the required classes.
I prefer xml configs over registry settings because I can simply make a class and use the xmlSerializer to open and save right into my classes.
Check this topic out for a similar SO question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With