Posts Tagged ‘Web.config’


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.


private void SetConfigSettings()
string path = Server.MapPath(“Web.config”);
string newValue = “New VAlue”;
XmlDocument xDoc = new XmlDocument();
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