How to access app configuration from a .dll?

前端 未结 5 1996
执念已碎
执念已碎 2021-02-09 10:42

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 s

5条回答
  •  长情又很酷
    2021-02-09 11:04

    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;
    

提交回复
热议问题