Actually, I'm a noob in working with files in asp.net and I need to implement a feature of " Editing web.config file at runtime." I know this sounds silly and I also know the consequences of this feature like it will restart the application but I really
need to do this.
Now, what I've thought for this is, showing all the content of web.config file in textbox and updating it with button placed just down the textbox.
Can anyone help me in this.
Thanks in Advance. [:)]
If any post helps you in any way, please don't hesitate to mark as answer because this will help others in finding the solution.
It gave error:
System.Web.HttpServerUtility' does not contain a definition for 'Mappath' and no extension method 'Mappath' accepting a first argument of type 'System.Web.HttpServerUtility' could be found (are you missing a using directive or an assembly reference?)
can you suggest some solution.
Thank you
If any post helps you in any way, please don't hesitate to mark as answer because this will help others in finding the solution.
pankajgoyal_...
Member
501 Points
166 Posts
Opening web.config file in Multiline Textbox control
Feb 04, 2012 07:14 PM|LINK
hi,
Actually, I'm a noob in working with files in asp.net and I need to implement a feature of " Editing web.config file at runtime." I know this sounds silly and I also know the consequences of this feature like it will restart the application but I really need to do this.
Now, what I've thought for this is, showing all the content of web.config file in textbox and updating it with button placed just down the textbox.
Can anyone help me in this.
Thanks in Advance. [:)]
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Opening web.config file in Multiline Textbox control
Feb 06, 2012 03:00 AM|LINK
Uisng LINQ-TO-XML can help you solve the problem easily——
XDocument doc = XDocument.Load("xxx.xml");
TextBox1.Text = doc.Root;
//Updating:
XDocument doc = XDocument.Parse(TextBox1.Text);
doc.Save(Request.Mappath("~/")+"web.config");
pankajgoyal_...
Member
501 Points
166 Posts
Re: Opening web.config file in Multiline Textbox control
Feb 19, 2012 08:00 AM|LINK
hi Decker Dong,
Sorry for late reply because I got sick really bad one day before your reply.
I need to open web.config file and when I wrote:
XDocument doc = XDocument.Load("~/web.config");
It gave an error : "System.IO.FileNotFoundException: Could not find file 'C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\web.config'."
and when I tried:
XDocument doc = XDocument.Load(Server.Mappath("~/web.config"));
It gave error: System.Web.HttpServerUtility' does not contain a definition for 'Mappath' and no extension method 'Mappath' accepting a first argument of type 'System.Web.HttpServerUtility' could be found (are you missing a using directive or an assembly reference?)
can you suggest some solution.
Thank you
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Opening web.config file in Multiline Textbox control
Feb 19, 2012 09:03 AM|LINK
maybe MapPath,please do that according to the intellisense tip,I only give you an example,don't copy directly。
And you should try to use Request.MapPath instead of Server.MapPath if still fails。
Reguards!
pankajgoyal_...
Member
501 Points
166 Posts
Re: Opening web.config file in Multiline Textbox control
Feb 19, 2012 04:08 PM|LINK
thnx for replying Decker,
I've managed to show web.config file on pageload event in textbox and on click button I'm updating the value.
Now the problem is, the code for updating file executes successfully but file doesn't get updated.
I've also written <httpRuntime requestValidationMode="2.0" >
Here's my code:
.aspx file:
<h1 class="h">Edit Web.Config File: </h1> <span style="border: solid red 1px; padding: 2px; margin: 3px; color: red; font-size: 12px;"> <b>NOTE: </b>Be careful! Web.config is easy to break! </span> <asp:TextBox ID="txtweb" runat="server" TextMode="MultiLine" Height="400px" Width="900px" Font-Underline="false"></asp:TextBox><br /> <br /> <asp:Button ID="btnupdate" runat="server" Text="Button" onclick="btnupdate_Click" /> <br />.aspx.cs file:
protected void Page_Load(object sender, EventArgs e) { load(); } void load() { XDocument doc = XDocument.Load(Request.MapPath("~/web.config")); txtweb.Text = doc.Root.ToString(); } protected void btnupdate_Click(object sender, EventArgs e) { //System.IO.StreamWriter sw = System.IO.File.CreateText(Request.MapPath("~/web.config")); //sw.Write(txtweb.Text); //sw.Close(); //sw.Dispose(); XDocument doc = XDocument.Parse(txtweb.Text); doc.Save(Request.MapPath("~/web.config")); load(); }Thank you
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Opening web.config file in Multiline Textbox control
Feb 20, 2012 12:19 AM|LINK
1)Please add if (!IsPostBack){……} and put the codes inside——
namespace WebAppCSharp { public partial class Default : System.Web.UI.Page { private void MyLoad() { XDocument doc = XDocument.Load(Request.MapPath("~/web.config")); TextBox1.Text = doc.Root.ToString(); } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { MyLoad(); } } protected void Button1_Click(object sender, EventArgs e) { XDocument doc = XDocument.Parse(TextBox1.Text); doc.Save(Request.MapPath("web.config")); MyLoad(); } } }2)Notice that you web.config should be dealt with this:
pankajgoyal_...
Member
501 Points
166 Posts
Re: Opening web.config file in Multiline Textbox control
Feb 20, 2012 08:14 AM|LINK
Thank you Decker,
I got my solution. [:)]
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Opening web.config file in Multiline Textbox control
Feb 21, 2012 12:19 AM|LINK
Glad to hear that,and hope you can come here more to chat or help others when free……
Reguards!