Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.
This question is asked many times on www.asp.net forums. Although its not a good idea to dynamically write anything in the web.config file reason being that writing anything in web.config will restart the application. Anyway, if for some reason you don't care about the restart and really wanted this functionality than you can look into this code:
private void GetConfigSettings() 
        {
            string path = Server.MapPath("Web.config"); 
            string newConnectionString = @"Server=local;Database="+txtDatabaseName.Text+";Trusted_Connection=true"; 

            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; // for key attribute
            xmlAttCollection[1].InnerXml = newConnectionString; // for value attribute
            
            xDoc.Save(path); // saves the web.config file         
        
        }

Posted on Sunday, October 9, 2005 11:26 AM | Back to top


Comments on this post: Dynamically Writing into Web.config File

# re: Dynamically Writing into Web.config File
Requesting Gravatar...
This is fine but i have another problem that i was working in VSS and my web.config file Readonly property is true... my qustion is how can i make that false thru program
Left by Naveen on Jun 24, 2006 12:23 AM

# re: Dynamically Writing into Web.config File
Requesting Gravatar...
really good and help ful code
Left by viral shah on Jun 30, 2007 4:46 AM

# re: Dynamically Writing into Web.config File
Requesting Gravatar...
this is not work,

access to the path is deneied when execute save command
Left by haja on Sep 18, 2007 4:54 AM

Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net