KnowDotNet Visual Organizer

Retrieve My.Settings From Loosely Coupled DLL in C# and VB.NET

Properties.Settings or My.Settings

by Les Smith
Print this Article Discuss in Forums

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

A call to the method would be as follows.

  string emailSvr = GetSettingValueFromAppConfigForDLL("CSTest", "EmailSvr");



The contents of the appConfig file are shown below.  You can see that the application name is used to qualify the section containing the applicationSettings section and why it has to be passed as a parameter to the method shown above.

<?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>

The VB.NET code for the method is shown below.  I complied this code successfully on one machine, but on the machine on which I am writing this article, I am getting compile errors having to do with the version of System.Configuration that is installed, so I hope you don't run into the same problem.

   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



Hopefully, this little code trick might come in handy someday.

Have you tried our newest product, Visual Class Organizer?  You'll be amazed how easy it is to keep the code in your code windows organized.  TRY IT FREE FOR 30 DAYS BY CLICKING HERE.



If you are developing in C# and haven't tried CSharpCompleter, you are wasting valuable time typing hundreds of braces {} daily needlessly.  Try CSharpCompleter for 30 DAYS FREE.



Ask a Question, or give your feedback on my articles or products by going to the KnowDotNet Forum or by clicking on My Blog.
  

Writing Add-Ins for Visual Studio .NET
Writing Add-ins for Visual Studio .NET
by Les Smith
Apress Publishing