How can I make a .NET class library read its own configuration file?

若如初见. 提交于 2019-11-28 18:50:24
s_hewitt

I think you're looking for:

ConfigurationManager.OpenExeConfiguration(string exePath)

or

ConfigurationManager.OpenMappedExeConfiguration(
    new ExeConfigurationFileMap() { 
        ExeConfigFilename = path + "app.config" 
    }, ConfigurationUserLevel.None);

Which returns a Configuration object. MSDN doc on ConfigurationManager

Try this question for how to get the DLL path.

A year out of date I know, but I used this method to read each of the settings:

Configuration config = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
ConfigurationSectionGroup csg = config.GetSectionGroup("applicationSettings");
ClientSettingsSection c = (ClientSettingsSection)csg.Sections["Add your section name here, e.g. Your.Namespace.Properties.Settings"];
foreach (SettingElement e in c.Settings)
{
    Debug.WriteLine("SETTING NAME: " + e.Name);
    SettingValueElement v = e.Value;
    Debug.WriteLine("SETTING VALUE: " + v.ValueXml.InnerText);
}

This works on a settings file created in a Class Library Project. The settings file should be named "YourLibrary.dll.config" and then deployed in the library's location. The settings file should have similar content to this:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="Your.NameSpace.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
    </sectionGroup>
  </configSections>
  <applicationSettings>
    <Your.NameSpace.Properties.Settings>
      <setting name="YourLibrary_WebReferences_YourWebService" serializeAs="String">
        <value>http://localhost:3861/YourWebService.asmx</value>
      </setting>
      <setting name="AnotherSetting" serializeAs="String">
        <value>False</value>
      </setting>
    </Your.NameSpace.Properties.Settings>
  </applicationSettings>
  <startup>
    <supportedRuntime version="v2.0.50727"/>
  </startup>
</configuration>

I haven't needed to read connection strings from the config file, but that should be possible by changing the name of the section group that you get after opening the exe configuration.

The reason why I needed to do this is that I have a User Control which wraps a ActiveX/COM library which is then hosted in IE in an "object" tag. I have got the use of "param" tags working, so I could have used that mechanism to have passed the settings into the User Control, but this method seemed a logical choice at the time. Plus I wasn't going to let this particular problem beat me!

HTH pridmorej :)

This kind of configuration issue is solved quite nicely using Enterprise Library "Shared Configuration Sources" and "Differential Configurations" (for easily switching between multiple environments, etc.).

To learn about Advanced Configuration Scenarios, try starting here:

http://msdn.microsoft.com/en-us/library/ff664552(v=pandp.50).aspx

And to integrate the Enterprise Library Configuration Tool (GUI) in Visual Studio, try here:

http://visualstudiogallery.msdn.microsoft.com/029292f0-6e66-424f-8381-3454c8222f9a

The learning curve may seem a bit overwhelming at first, but it is well worth the effort, especially if you are dealing with complex enterprise ecosystems. The integrated tool actually makes it pretty easy to set up a very sophisticated configuration system, and manage it as your requirements change.

BTW: Once you get the hang of it, you'll probably end up wanting to use it for a lot more than just your Connection Strings!

The ConfigurationManager.OpenExeConfiguration didn't work for me.

But, Properties.Settings.Default.<SettingName> worked well.

nodots

Would it be an option to reference the library's configuration file from each web service's configuration? There is an XML include-like mechanism in .NET:

Use XML includes or config references in app.config to include other config files' settings

http://blog.andreloker.de/post/2008/06/Keep-your-config-clean-with-external-config-files.aspx

While you'd still need to edit each web.config, the actual content is maintained in a single place.

You can use the opensource Config.Net library which supports multiple sources of data, including your one https://github.com/aloneguid/config

Sumit Bhattacharyya

You can read the configuration settings of the class library from the hosting application's web.config or app.config.

If the class library is referenced in a console application, put the settings, the class library needs, in the app.config of the console application (say under appSettings) and read it from the class library using ConfigurationManager under System.Configuration.

If the class library is referenced in a web application, put the settings the class library needs in the web.config of the web application (say under appSettings) and read it from the class library using ConfigurationManager under System.Configuration.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!