Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the web.config file in the root of c://inetpub/wwroot/Example/Web.config. This web.config file contains ConnectionStrings and Appsettings value. Now i want to modify the web.config appsettings and connection strings value by programmatically using c#. Pleas help me.

Edit -
[ OP's additional comments moved here from Solution section. ]

I have the web.config file in c://Inetpub/wwwroot/MyInstaller. When i am opening a file Its throwing error like "The relative virtual path 'C:/Inetpub/wwwroot/MyInstaller' is not allowed here.
Posted
Updated 21-Nov-11 3:25am
v2

 
Share this answer
 
Comments
RaviRanjanKr 21-Nov-11 9:33am    
My 5+
RaisKazi 21-Nov-11 9:44am    
Thank you Ravi.
This file is an XML file, and reading/writing XML is well supported by .NET. Choose from the methods I overview below:

XML Read/Write:

  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^].
  2. Use the classes System.Xml.XmlTextWriter and System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx[^], http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^].
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].


As this file is usually quite small, I would choose XmlDocument, which is probably the easiest. You will have to read the whole file into DOM, do you changed using DOM interface and then save the whole file.

—SA
 
Share this answer
 
Comments
RaviRanjanKr 21-Nov-11 9:33am    
My 5+
Sergey Alexandrovich Kryukov 21-Nov-11 10:21am    
Thank you, Ravi.
--SA
RaisKazi 26-Nov-11 7:25am    
My 5.
Sergey Alexandrovich Kryukov 26-Nov-11 19:23pm    
Thank you, Rais.
--SA
Did you try google? Lots of examples are available.

You need to use WebConfigurationManager and open the config file. And later get the section, which you want to modify and update it. refer the links below

Click here

Click here 2
 
Share this answer
 
Comments
RaviRanjanKr 21-Nov-11 9:32am    
My 5+
I have the web.config file in c://Inetpub/wwwroot/MyInstaller. When i am opeing a file Its throowing error like "The relative virtual path 'C:/Inetpub/wwwroot/MyInstaller' is not allowed here.
 
Share this answer
 
Comments
RaisKazi 21-Nov-11 9:22am    
Please do not add your comments as a Solution. Instead update your details using "Improve Question" link just below your Question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900