Configuration files are just great they let you configure the website (pretty obvious huh). It's a great place to store the information which you are going to be using throughout the application and which is not changing. In this article I will show you that how you can extract the information from the configuration file using API's provided by the .NET Framework 2.0. I will also show you that how easily you can encrypt the sections of the configuration files.

Introduction:

Configuration files are just great they let you configure the website (pretty obvious huh). It's a great place to store the information which you are going to be using throughout the application and which is not changing. In this article I will show you that how you can extract the information from the configuration file using API's provided by the .NET Framework 2.0. I will also show you that how easily you can encrypt the sections of the configuration files.


Extracting Information from the Configuration Files Programmatically:

Let's first see how we can extract the information from the tags that are defined in the configuration files. Let's say our configuration files contains the <appSettings> section which looks something like this:

<appSettings>

<add key="SQLProvider" value="AzamSharp.Data.SqlClient" />

<add key="SQLConnection" value="System.Data.SqlClient.SqlConnection" />

<add key="OracleProvider" value="System.Data.OracleClient" />

</appSettings>

Now, if you want to get all the keys you can easily use the AllKeys property of the AppSettingsSection. Take a look at the code below:

Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);

AppSettingsSection appSettings = (AppSettingsSection) config.GetSection("appSettings");

string[] appKeys = appSettings.Settings.AllKeys;

for (int i = 0; i < appSettings.Settings.Count; i++)

{

Response.Write(appSettings.Settings[appKeys[i]].Value);

Response.Write("<BR>");

}

The above code will simply print out the value of all the keys in the AppSettings section of the configuration file.

If you want to extract just a single value based on its key name then you can simply use the following code:

Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);

AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");

string msSqlProvider = appSettings.Settings["SQLProvider"].Value;

Pretty simple right!

I am using the above example with the appSettings section of the configuration file but you can use it with most of the other sections of the web.configuration file.

Extracting ConnectionString Dynamically:

Let's try the same thing with the connection strings stored in web.config file. Check out the connection string settings below:

<appSettings>

<add key="SQLProvider" value="AzamSharp.Data.SqlClient" />

<add key="SQLConnection" value="System.Data.SqlClient.SqlConnection" />

<add key="OracleProvider" value="System.Data.OracleClient" />

</appSettings>

I can easily extract all of the connection strings using the following code:

Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);

 

ConnectionStringsSection conSection = (ConnectionStringsSection)config.GetSection("connectionStrings");

ConnectionStringSettingsCollection conCollection = conSection.ConnectionStrings;

foreach (ConnectionStringSettings conSetting in conCollection)

{

Response.Write(conSetting.ConnectionString);

Response.Write("<BR>");

}

You can also extract a single connection using the conSection object which is of ConnectionStringSection type.

Writing Connection String Dynamically:

You can also write the connection string back to the Web.config dynamically. In ASP.NET 1.X this was done using the XmlDocument class and iterating through the XML tags of Web.config (Writing to Web.config file dynamically). In ASP.NET 2.0 this is much easier take a look at the code below where I dynamically change the value of the connection string.

Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);

 

ConnectionStringsSection conSection = (ConnectionStringsSection)config.GetSection("connectionStrings");

conSection.ConnectionStrings["SQLConnectionString"].ConnectionString = "NewConnectionString";

config.Save();

 

Please note that when you write to a web.config file the application restarts which means all the session and application variables are lost.


Encrypting Config Sections:

In ASP.NET 2.0 you can also easily encrypt the config sections dynamically. Let's see how we can encrypt the connection strings section.

Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);

ConfigurationSection configSection = config.GetSection("connectionStrings");

if (configSection.SectionInformation.IsProtected)

{

configSection.SectionInformation.UnprotectSection();

config.Save();

}

else

{

configSection.SectionInformation.ProtectSection

("DataProtectionConfigurationProvider");

config.Save();

}

First we check that if the section is already encrypted if it is then we decrypt it else we encrypt it. Take a look at the resulting connection string section in web.config.

<connectionStrings configProtectionProvider="DataProtectionConfigurationProvider">
<EncryptedData>
<CipherData>
<CipherValue>AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAVClqG40BZkCjK40

adynN8gQAAAACAAAAAAADZgAAqAAAABAAAABIhtOWxFnMKjMa8/cR5tYzAAA

AAASAAACgAAAAEAAAAPfywOeewrUxUGUKp+WpCPE</CipherData>
</EncryptedData>
</connectionStrings>

I hope you liked this article, happy coding!

If you are one of the thousands that visit GridViewGuy for your .NET articles and resources, you might be interested in making a donation. Extra cash helps pay for the hosting services and speed things up around here, and makes this website possible.

Make a Donation

Once, again thank you very much and remember its because of you FINE people that this website is up and running.

 

Export Button is a custom control that let's you export your DataGrid or TextBox data to several different formats. The control is extremely easy to use and also exposes design time features. In this article I will discuss some of the features of the Export Button and how it benefits the developer.

BUY IT NOW