h1

Dynamically Writing into Web.config File

July 13, 2010

Dynamically Writing into Web.config File is not good practice because dynamically write anything in the Web.config file application will restart. Anyway you don’t bother about the application restart can do like this.

Coding:

private void SetConfigSettings()
{
string path = Server.MapPath(“Web.config”);
string newValue = “New VAlue”;
XmlDocument xDoc = new XmlDocument();
xDoc.Load(path);
XmlNodeList nodeList = xDoc.GetElementsByTagName(“appSettings”);
XmlNodeList nodeAppSettings = nodeList[0].ChildNodes;
XmlAttributeCollection xmlAttCollection = nodeAppSettings[0].Attributes;
xmlAttCollection[0].InnerXml = txtKey.Text; // key attribute
xmlAttCollection[1].InnerXml = newConnectionString; // value attribute
xDoc.Save(path); // save the web.config file
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: