Skip to main content

How to use a custom config file?

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 -

  • 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

Popular posts from this blog

Notes on Castle MonoRail

  Sometime back I was doing a small POC on Castle MonoRail. So here are my quick notes on this. MonoRail is an MVC Framework from Castle inspired by ActionPack. MonoRail enforces separation of concerns with Controller handling application flow, models representing data and View taking care of the presentation logic. To work with MonoRail you need Castle Assemblies. It also utilizes nHibernate You can use Castle MonoRail Project Wizard or create the project manually. Project structure – Content Css Images Controllers HomeController.cs Models Views Home \ index.vm Layouts \ Default.vm ...

Workflow Foundation 4 - Part 3 - Data storage and management

This is my third post on WF4. First one was an introductory post on WF4 and in second one we focused on executing workflows. In the this post I am going to focus on the topic of data storage and management. Every business process or flow depends on data. When you think of data there are three elements to it as listed below - Variables - for storing data Arguments - for passing data Expressions - for manipulating data. Let us first look at the variables. Variables are storage locations for data. Variables are declared before using them just like in any other languages like C# or VB.Net. Variables are defined with a specific scope. When you create a variable in an activity the scope of the variable becomes that activity's scope. Variables can also have access modifiers like None, Mapped or ReadOnly. Let us look at an example where we will create two variables and assign a scope to them along with access modifiers. //Declare a sequence activitiy Sequence seqWf = new Sequence(); //de...

Introduction to Workflow Foundation 4 (WF4)

I finally decided to pick-up my blogging once more. Since I have been trying to learn Windows Workflow Foundation 4 (WF4) I thought I might as well start off with this topic. WF4 is a development framework that enables you to create a workflow and embed it in a .Net application. It is neither an executable application nor a language. It provides a set of tools for declaring a workflow, activities to create your logic and to define control flow and a runtime for executing the resulting application definition. It also provides services for persistence of state, tracking and bookmarking. You can create your workflow directly in code, in mark-up or in a combination of both. Workflow provide us with two major advantages - Creating unified application logic. Making application logic scalable. Workflow Authoring styles - Sequential Workflow executes a set of contained activities in a sequential manner. Workflow Foundation was introduced in the .Net 3.0 and updated in 3.5. In .net 4 it has bee...