Friday, July 14, 2006

Editing Web.config file from code behind


Hi friends,
Some times you might have thought that it would make things convenient to write back to your .NET application's config file. The framework provides simple methods for reading from the config file, but gives you nothing for writing values back to the config file. It is easy enough to write values back to the file. It's only XML file.

I am using framework 2.0. Let’s we will make one application in ASP.NET. Our application contains the “web.config” file and our application aim is to modify the config file. For that we will add one key in “appsetting” section of the config file as shown below. We will allow application to change the value of the key “connectionstring” from “sandy” to “testapplication”.

Change/ add in web.config:

<appSettings>
<add key="ConnectionString" value="sandy" />
</appSettings>

To write values back, you just need to open the config file as an XmlDocument and write away. No big deal. You can add name/value pairs, remove elements, and modify them or whatever. Copy the code and paste it in the codebehind of the page. This code will allow you to change “appSettings” section of the config file based on the key name.

For this you need to make the aspx page for the front end. With the one text box demanding for the value to change and submit button.

Code is given below:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Xml;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
string ConnectionString;
ConnectionString = txtvalue.Text.ToString(); // or else you can take the parameters from the form
//and create your own connectionstring
// like string ConnectionString = @"Server=local;Database=" + txtDatabaseName.Text + ";Trusted_Connection=true";

// set the web.config path to read / change
string path = Server.MapPath("Web.config"); // root web.config

// we will search for the appSetting entity
XmlDocument xDoc = new XmlDocument();

xDoc.Load(path); // load the web.config

XmlNodeList nodeList = xDoc.GetElementsByTagName("appSettings");

XmlNodeList nodeAppSettings;

XmlNode node;

// search the key
nodeAppSettings = nodeList[0].ChildNodes; // get all the nodes present under
// appSetting

node = nodeList[0].ChildNodes[0]; // take the First node i.e. [add] node with [key name] ConnectionString
// [Add] - Node name
// [Key] - Attribute Name
// [Value] - Attribute Name

//int nnodes;
//for (nnodes = 0; nnodes <= nodeList.Count - 1; nnodes++)
//{
// if(nodeList[0].ChildNodes.
// nodeAppSettings = nodeList[0].ChildNodes;
//}

XmlAttributeCollection xmlAttCollection = node.Attributes;

// you can even change the key name
//xmlAttCollection[0].InnerXml = txtKey.Text; // for key attribute
xmlAttCollection[1].InnerXml = ConnectionString.ToString(); // for value attribute

// Writting Web.config file
xDoc.Save(path); // saves the web.config file
}
}



Note: it is not good to write back to the config file. The framework does not include this ability for a reason.

No comments: