Introduction
Generally there could be a requirement such that any independent component needs to be having its own configuration settings like for example, let say I have a vc++ application which acts as a container so that it can load any Active X
Control in it. Now as a new technology I want to use some plugin to be built in Visual C# Language using .NET Framework. As we already know we can build the Active X Component using .NET Framework. Now let’s say my component plugin need to interact with 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 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 can we map this config file even when it is converted to Active X Control and running in other environment.
Background
Need to have a basic knowledge in creating Active X Control or look for my next article “How to create an Active X Control using .NET Framework”.
Explanation
I don’t want to make it complex for you, as we are on path what we want now by reading the Introduction section.
We may have other options like maintaining 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 ig from my side I named as “ChangeMyAppConfig” and this class need to be
Implement “AppConfig” Class.
01.
public
class
ChangeAppConfig : AppConfig
02.
{
03.
private
readonly
string
oldConfig =
04.
AppDomain.CurrentDomain.GetData(
"APP_CONFIG_FILE"
).ToString();
05.
06.
private
bool
disposedValue;
07.
08.
public
ChangeAppConfig(
string
path)
09.
{
10.
AppDomain.CurrentDomain.SetData(
"APP_CONFIG_FILE"
,
path);
11.
ResetConfigMechanism();
12.
}
13.
"APP_CONFIG_FILE",
path);
e style="color:#5c5c5c;float:left;width:3em;padding-right:0.3em;text-align:right;display:block;">14.
public
override
void
Dispose()
15.
{
16.
if
(!disposedValue)
17.
{
18.
AppDomain.CurrentDomain.SetData(
"APP_CONFIG_FILE"
,
oldConfig);
19.
ResetConfigMechanism();
20.
21.
22.
disposedValue
=
true
;
23.
}
24.
GC.SuppressFinalize(
this
);
25.
}
26.
27.
private
static
void
ResetConfigMechanism()
28.
{
29.
typeof
(ConfigurationManager)
30.
.GetField(
"s_initState"
,
BindingFlags.NonPublic |
31.
BindingFlags.Static)
32.
.SetValue(
null
,
0);
33.
34.
typeof
(ConfigurationManager)
35.
.GetField(
"s_configSystem"
,
BindingFlags.NonPublic |
36.
BindingFlags.Static)
37.
.SetValue(
null
,
null
);
38.
BindingFlags.Static)
37.
span>
39.
typeof
(ConfigurationManager)
40.
.Assembly.GetTypes()
41.
.Where(x
=> x.FullName ==
42.
"System.Configuration.ClientConfigPaths"
)
43.
.First()
44.
.GetField(
"s_current"
,
BindingFlags.NonPublic |
45.
BindingFlags.Static)
46.
.SetValue(
null
,
null
);
47.
}
48.
}
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 AppDomain.GetData() method and set our new configuration file path using 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:
1.
public
string
strCommonFolder = ConfigurationManager.AppSettings[
"FolderPath"
];
Have a Happy Coding :)