Click here to Skip to main content
12,500,884 members (45,710 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET
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 20-Nov-11 20:37pm
Updated 21-Nov-11 3:25am
RaisKazi34.4K
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
RaviRanjankr 21-Nov-11 9:33am
   
My 5+
RaisKazi 21-Nov-11 9:44am
   
Thank you Ravi.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Comments
RaviRanjankr 21-Nov-11 9:33am
   
My 5+
SAKryukov 21-Nov-11 10:21am
   
Thank you, Ravi.
--SA
RaisKazi 26-Nov-11 7:25am
   
My 5.
SAKryukov 26-Nov-11 19:23pm
   
Thank you, Rais.
--SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
Comments
RaviRanjankr 21-Nov-11 9:32am
   
My 5+
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

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.
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160919.1 | Last Updated 21 Nov 2011
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100