How can I retrieve settings of an executable's appConfig from a loosely coupled DLL? This article will show you in VB.NET and C#.
I have been developing several loosely coupled plug-in DLLs which are called by a main application. The plug-ins implement an interface, but the plug-ins themselves are not the subject of this article. In the development of the plug-ins, I found that I needed to access values in the appConfig file of the application that calls the plug-ins. In the initial design of the interfaces, I thought that I had included everything in the method signatures that was going to be needed. After I had several of the plug-ins in production, I found that I needed some values from the applications's appConfig that were not passed to the plug-in and I certainly did no want to break the production plug-ins by adding to the Interface.
I wrote the following code to retrieve the settings from the appConfig directly. I also did not want to have to add special sections to the appConfig which could be simply accessed by creating an appConfig in the in plug-in project and then copying the section to the application's appConfig. I just wanted to retrieve settings that the application would retrieve by the use of Properties.Settings (C#) or My.Settings (VB). The following code will do the trick.
Before you use this code, you must add a reference to the System.Coniguration Namespace and then Import (VB) or using System.Configuration (C#). You must add the reference, not just import the namespace.
The C# code is shown below:
| /// <summary> /// Returns value for supplied name. This method allows a DLL to /// search an appConfig file for Properties.Settings used by the calling /// application. This allows the application config file to be a container /// for settings that a DLL may access but which can be accessed by the /// application with My.Settings (VB) or Properites.Settings (C#). Obviously, /// the DLL's values could be set up in the appConfig for that DLL and transferred /// to the app's appConfig, but this simplier form does not require the appConfig /// to have a different section and its associated sectionGroup name. And, in this /// case we simply want to access settings that are already in the appConfig but /// were not passed to the DLL. I.e., it allows the DLL to access the appConfig /// as if it were the exe accessing its appConfig. /// </summary> /// <param name="appName"></param> /// <param name="searchName"></param> /// <returns>string</returns> private string GetSettingValueFromAppConfigForDLL(string appName, string searchName) { 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; } } } |
| string emailSvr = GetSettingValueFromAppConfigForDLL("CSTest", "EmailSvr"); |
| <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" > <section name="CSTest.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </sectionGroup> </configSections> <applicationSettings> <CSTest.Properties.Settings> <setting name="EmailSvr" serializeAs="String"> <value>mail.KDN.net</value> </setting> <setting name="Production" serializeAs="String"> <value>yes</value> </setting> </CSTest.Properties.Settings> </applicationSettings> </configuration> |
| Private Function GetSettingValueFromAppConfigForDLL(ByVal appName As String, ByVal settingName As String) Dim sectionName As String = "applicationSettings/" & appName & ".My.MySettings" Dim section As System.Configuration.ClientSettingsSection = _ DirectCast(System.Configuration.ConfigurationManager.GetSection(sectionName), _ System.Configuration.ClientSettingsSection) For Each setting As SettingElement In section.Settings Dim value As String = setting.Value.ValueXml.InnerText Dim name As String = setting.Name If name.ToLower.StartsWith(settingName) Then Return value End If Next Return String.Empty End Function |
| Ask a Question, or give your feedback on my articles or products by going to the KnowDotNet Forum or by clicking on My Blog. | ![]() |