Recently I was wondering how to use custom configuration files in my application. So here it is all about custom configuration file
First of all let me clarify what I mean by custom configuration files?
For me it was simply about using customfile.config instead of the usual app.config and or web.config.
In-order to use customfile.config instead of app.config in an application lets create a simple console application. After you have created a simple console application right click the project in solution explorer window and click add new item. From the list of options available select the Application Configuration File. Now rename the app.config name to customfile.config and click ok. Open the customfile.config.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
</configuration>
Lets add some configuration elements that are most common to our config files.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="SQLCon" connectionString="" providerName=""/>
</connectionStrings>
<appSettings >
<add key="Key1" value="ABC" />
<add key="Key2" value="XYZ" />
</appSettings >
</configuration>
Now that we have laid our ground work let begin to write code to access this custom config file and its configuration elements. To do this we need to follow the below mentioned steps -
//create an instance of exe configuration file map type
ExeConfigurationFileMap configFileMap =
new ExeConfigurationFileMap();
//assign the custom config file's name here
configFileMap.ExeConfigFilename = "customfile.config";
//now get an instance of configuration type
// use ConfigurationManager's static method
// OpenMappedExeConfgiuration
Configuration configInstance =
ConfigurationManager.OpenMappedExeConfiguration
(configFileMap, ConfigurationUserLevel.None);
//access appsettings section
configInstance.AppSettings.Settings["Key1"].Value;
//access connection string
configInstance.ConnectionStrings.ConnectionStrings["SQLCon"]
.ConnectionString;
For using this custom config file in a web application the process is absolutely same with the only difference in assigning the filename to ExeConfigFilename property. Here we need to use HTTPContext.Current.Server.MapPath("customfile.config").
//create an instance of exe configuration file map type
ExeConfigurationFileMap configFileMap =
new ExeConfigurationFileMap();
//assign the custom config file's name here
configFileMap.ExeConfigFilename =
HTTPContext.Current.Server.MapPath("customfile.config");
//now get an instance of configuration type
// use ConfigurationManager's static method
// OpenMappedExeConfgiuration
Configuration configInstance =
ConfigurationManager.OpenMappedExeConfiguration
(configFileMap, ConfigurationUserLevel.None);
//access appsettings section
configInstance.AppSettings.Settings["Key1"].Value;
//access connection string
configInstance.ConnectionStrings.ConnectionStrings["SQLCon"]
.ConnectionString;
First of all let me clarify what I mean by custom configuration files?
For me it was simply about using customfile.config instead of the usual app.config and or web.config.
In-order to use customfile.config instead of app.config in an application lets create a simple console application. After you have created a simple console application right click the project in solution explorer window and click add new item. From the list of options available select the Application Configuration File. Now rename the app.config name to customfile.config and click ok. Open the customfile.config.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
</configuration>
Lets add some configuration elements that are most common to our config files.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="SQLCon" connectionString="" providerName=""/>
</connectionStrings>
<appSettings >
<add key="Key1" value="ABC" />
<add key="Key2" value="XYZ" />
</appSettings >
</configuration>
Now that we have laid our ground work let begin to write code to access this custom config file and its configuration elements. To do this we need to follow the below mentioned steps -
- Add a reference to System.Configuration.dll
- Add a using statement on the top to import System.Configuration namespace
- Now create an object of type ExeConfigurationFileMap and instantiate it using the new keyword
- This object of type ExeConfigurationFileMap exposes a property called ExeConfigFilename. Assign the name of your custom configuration file to this property
- Now use the ConfigurationManager to call the method OpenMappedExeConfiguration by passing to it an instance of the above created ExeConfigurationFileMap and a configuration user level enum as ConfigurationUserLevel.None
- Above call to open a mapped exe configuration will return an instance of Configuration object which you can now use to access your configuration elements as usual.
//create an instance of exe configuration file map type
ExeConfigurationFileMap configFileMap =
new ExeConfigurationFileMap();
//assign the custom config file's name here
configFileMap.ExeConfigFilename = "customfile.config";
//now get an instance of configuration type
// use ConfigurationManager's static method
// OpenMappedExeConfgiuration
Configuration configInstance =
ConfigurationManager.OpenMappedExeConfiguration
(configFileMap, ConfigurationUserLevel.None);
//access appsettings section
configInstance.AppSettings.Settings["Key1"].Value;
//access connection string
configInstance.ConnectionStrings.ConnectionStrings["SQLCon"]
.ConnectionString;
For using this custom config file in a web application the process is absolutely same with the only difference in assigning the filename to ExeConfigFilename property. Here we need to use HTTPContext.Current.Server.MapPath("customfile.config").
//create an instance of exe configuration file map type
ExeConfigurationFileMap configFileMap =
new ExeConfigurationFileMap();
//assign the custom config file's name here
configFileMap.ExeConfigFilename =
HTTPContext.Current.Server.MapPath("customfile.config");
//now get an instance of configuration type
// use ConfigurationManager's static method
// OpenMappedExeConfgiuration
Configuration configInstance =
ConfigurationManager.OpenMappedExeConfiguration
(configFileMap, ConfigurationUserLevel.None);
//access appsettings section
configInstance.AppSettings.Settings["Key1"].Value;
//access connection string
configInstance.ConnectionStrings.ConnectionStrings["SQLCon"]
.ConnectionString;
Comments