.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Code Snippets >> File Operation >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

update information to the web.config at run time.

Posted By:nishithraj       Posted Date: December 04, 2009    Points: 10    Category: File Operation    URL: http://www.dotnetspark.com  

update information to the web.config at run time. We can update the appsetting elements of web.config file even at the runtime. If we require to update Filepath to the web.config, we can't do the hard coding. Because the installation path can change depending on the user selects. This code snippet will help you to achieve this.


It checks for each XML elements in the web.config file. Also it updates once it finds it. But one important thing to remember before implementing is to remove all the xml comments from the config file.


Following is the code.

protected void Page_Load(object sender, EventArgs e)

        string strDirectoryPath = Server.MapPath("ExcelReports");

        XmlDocument xmlDoc = new XmlDocument();



        foreach (XmlElement element in xmlDoc.DocumentElement)


            if (element.Name.Equals("appSettings"))


                foreach (XmlNode node in element.ChildNodes)


                    if (node.Attributes[0].Value.Equals("FilePath"))


                        node.Attributes[1].Value = strDirectoryPath;








And In Web.config




Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend