8

I recently broke out a part of my winform app in a .dll. Some of the classes in that dll wants fetch/store user settings. The classes just used the VS generated Settings file so it just did Properties.Settings.Default.SomeSetting = var;Properties.Settings.Default.Save() etc.

What are my options now that I moved that code out to a class library/.dll ?

leeeroy
  • 11,216
  • 17
  • 52
  • 54
  • Are you looking for a method of accessing the previous settings or are you under the impression that class libraries can't have app.config files? – Austin Salonen Feb 22 '10 at 19:42
  • I don't know what I'm looking for, that's why I ask. I can't get my dlls to access its seperate app.config though, nor does clickonce deployment form visual studio include a .dll's settings file. – leeeroy Feb 22 '10 at 19:50

5 Answers5

3

The hosting application should handle the interface to the config file, not the DLL. Either

  1. Pass whatever settings need to be read/modified within the DLL as parameters, or

  2. Pass in a name-value collection of settings that can be modified by the DLL, and save whatever changes are made by the DLL to the collection when control returns to the calling application.

This is similar in principle to removing a database interface from the business layer of a tiered application and encapsulating it into a data layer.

3Dave
  • 28,657
  • 18
  • 88
  • 151
2

It doesn't make a lot of sense to me to have a DLL storing user settings. A DLL is a library, not an application, and doesn't directly interact with the user. If classes in the DLL need access to user settings, you can pass them in as parameters.

Instance Hunter
  • 7,837
  • 5
  • 44
  • 56
  • 2
    DLL are not only libraries. You have DLL that are plugins and have different interfaces that can hot swap while the main app is running. If plugin need network credential or something like that you need the DLL to be able to read configuration set into the main application. You are not to change the constructor of thousands of plugins because one of them need an extra value – Franck Sep 14 '17 at 13:41
  • What about separation of concerns? Shouldn't all libraries manage their own configuration? I think they should. – kwitee Apr 30 '21 at 08:05
2

The Properties class is autogenerated. It is really a wrapper on the config file. If you don't want to change your design, just go into the code and copy it to your DLL. But remember it will no longer be magically maintained (regenerated). Or you can use ConfigurationManager to get at config file directly.

Jennifer Zouak
  • 1,338
  • 6
  • 12
1

I would not recommand it (better use your own class for settings), but you can try this:

string sectionName = "applicationSettings/" + 
            appName + ".Properties.Settings";
         System.Configuration.ClientSettingsSection section = 
            (System.Configuration.ClientSettingsSection)
             System.Configuration.ConfigurationManager.GetSection(sectionName);
         foreach (SettingElement setting in section.Settings)
         {
            string value = setting.Value.ValueXml.InnerText;
            string name = setting.Name;
            if (name.ToLower().StartsWith(searchName.ToLower()))
            {
               return value;
            }
         }
Igor
  • 278
  • 1
  • 7
0

For those who need to read settings from userDirectory/user.config, here is a solution:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
ConfigurationSectionGroup userSettings = config.GetSectionGroup("userSettings");
ClientSettingsSection settings = (ClientSettingsSection)userSettings.Sections.Get("[applicationName].Properties.Settings");
SettingElement elem = settings.Settings.Get([settingName]);
var sett = elem.Value.ValueXml.InnerText;
eldarerathis
  • 35,455
  • 10
  • 90
  • 93