How to overwrite (DO NOT add) a text file in ASP.NET using C #

I have included a text file on my website with several lines. I placed a text box (Multimode = true) and a button on the page. In Page_Load, the contents of the text field should appear in the text field. Then the user can edit the text field. When the button is clicked, the current contents of the TextBox should be overwritten in this text file (it should not be added).

I successfully display text file data in a text box. But when overwriting, it is added to the text file, and not overwritten.

This is my code:

protected void Page_Load(object sender, EventArgs e) { if (File.Exists(Server.MapPath("newtxt.txt"))) { StreamReader re = new StreamReader(Server.MapPath("newtxt.txt")); while ((input = re.ReadLine()) != null) { TextBox1.Text += "\r\n"; TextBox1.Text += input; } re.Close(); } else { Response.Write("<script>alert('File does not exists')</script>"); } } protected void Button1_Click(object sender, EventArgs e) { StreamWriter wr = new StreamWriter(Server.MapPath("newtxt.txt")); wr.Write(""); wr.WriteLine(TextBox1.Text); wr.Close(); StreamReader re = new StreamReader(Server.MapPath("newtxt.txt")); string input = null; while ((input = re.ReadLine()) != null) { TextBox1.Text += "\r\n"; TextBox1.Text += input; } re.Close(); } 

How to overwrite a text file and then display it in the text box with the same click of a button?

+4
source share
4 answers

Check out the System.IO.File.WriteAllText method, it will overwrite the file if it exists, and all this is done in only one line of code. Similarly, you can use the System.IO.File.ReadAllText method to easily get the contents of a file.

 protected void Page_Load(object sender, EventArgs e) { if (File.Exists(Server.MapPath("newtxt.txt"))) { TextBox1.Text = System.IO.File.ReadAllText("newtxt.txt"); } else { Response.Write("<script>alert('File does not exists')</script>"); } } protected void Button1_Click(object sender, EventArgs e) { System.IO.File.WriteAllText("newtxt.txt", TextBox1.Text); } 
+3
source

StreamWriter Constructor has several overloads, including one to indicate whether to add or overwrite.

 StreamWriter wr = new StreamWriter(Server.MapPath("newtxt.txt"), false); 

From MSDN, the second parameter:

Determines whether data should be added to the file. If the file exists and append is false, the file is overwritten. If the file exists and the addition is true, data is added to the file. Otherwise, a new file is created.

+6
source

Server.MapPath returns a string indicating the path to the file. You can try to open the file manually before transferring it to the stream author. Pay attention to FileMode Create, and FileAccess of Write .

 var path = Server.MapPath("newtxt.txt"); using (var fileStream = File.Open(path, FileMode.Create, FileAccess.Write)) { using (var writer = new StreamWriter(fileStream)) { // the rest of your code } } 
+5
source

It seems you are overwriting the file in the Click Button handler, and then adding its contents to the TextBox. Thus, the client adds a file to the file.

Try the following in the Click Button handler after writing the file and before reading it:

 TextBox1.Text = ""; 

Or just don’t bother to read - it makes no sense, because the text you wrote in the file is still in the text field.

+1
source

Source: https://habr.com/ru/post/1347338/


All Articles