Making Our Component to be Dynamically Loading the Config File

Introduction

Generally there could be a requirement such that an independent component needs its own configuration settings like for example, let's say I have a VC++ application which acts as a container so that it can load an Active X Control in it. Now as a new technology I want to use a plugin to be built in the Visual C# Language using the .NET Framework. As we already know, we can build the Active X Component using the .NET Framework. Now let's say my component plugin needs to interact with the Database, so I need to provide it a Connection String and I can't hard-code it inside my Component so I decided to have an application configuration file (i.e., App.Config file), but how can I map this file to my plugin when it is running in my VC++ container?

This article explains how we can solve this issue, and how we can map this config file even when it is converted to an Active X Control and running in another environment.

Background

You need to have a basic knowledge of creating an Active X Control or look for my next article "How to Create an Active X Control Using the .NET Framework".

Explanation

I don't want to make it too complex for you, since we are on the subject specified in the Introduction section.
We may have other options like maintaining a separate XML file and have all the complex custom access through the XML file using xpath technology. But the way we access this configuration file using System.

Configuration.ConfigurationManager Class is very Flexible and comfortable.

The solution is simple; we need to create a class and name it; for my example I used the name "ChangeMyAppConfig" and this class is derived frm the "AppConfig" Class.

public class ChangeAppConfig : AppConfig
{
    private readonly string oldConfig =
        AppDomain.CurrentDomain.GetData("APP_CONFIG_FILE").ToString();

    private bool disposedValue;

    public ChangeAppConfig(string path)
    {
        AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", path);
        ResetConfigMechanism();
    }

    public override void Dispose()
    {
        if (!disposedValue)
        {
            AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", oldConfig);
            ResetConfigMechanism();
 
            disposedValue = true;
        }
        GC.SuppressFinalize(this);
    }

    private static void ResetConfigMechanism()
    {
        typeof(ConfigurationManager)
            .GetField("s_initState", BindingFlags.NonPublic |
                                     BindingFlags.Static)
            .SetValue(null, 0);

        typeof(ConfigurationManager)
            .GetField("s_configSystem", BindingFlags.NonPublic |
                                        BindingFlags.Static)
            .SetValue(null, null);
        typeof(ConfigurationManager)
            .Assembly.GetTypes()
            .Where(x => x.FullName ==
                        "System.Configuration.ClientConfigPaths")
            .First()
            .GetField("s_current", BindingFlags.NonPublic |
                                   BindingFlags.Static)
            .SetValue(null, null);
    }
}

{

The main AppDomain has the property called "APP_CONFIG_FILE" where it stores the configuration file information. Now we need to fetch this property using the AppDomain.GetData() method and set our new configuration file path using the AppDomain.SetData() Method. It's not enough to change the value of the property; with this we need to reset the configuration mechanism as we are doing the same in ResetConfigMechanism() method in the above code.

That's it. Now you need to call the ChangeAppConfig() Method from your component by providing the valid configuration path.

After calling this method you can now access the Configuration file settings using System.

Configuration.ConfigurationManager Class as we do normally in every application in .NET Environment.

Example:

public string strCommonFolder = ConfigurationManager.AppSettings["FolderPath"];
 


Similar Articles