How to access app configuration from a .dll?

前端 未结 5 1992
执念已碎
执念已碎 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:15

    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;
                }
             }
    

提交回复
热议问题