title: How to overwrite (NOT append) a text file in ASP.NET using C#
body: <p>I have included a text file in my website with multiple lines.
  I have put a textbox (Multimode=true) and a button in the page.
  On Page_Load the content from the textFile should be displayed in the textbox.
  Then the user can edit the textbox. On button click the current content of TextBox should be overwritten in that text file (it should not be appended).</p>
  
  <p>I'm successfully displaying the text file data in a textbox. But while overwriting, it appends in the text file rather than overwriting.</p>
  
  <p>This is my code:</p>
  
  <pre><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("&lt;script&gt;alert('File does not exists')&lt;/script&gt;");
      }       
  }
  
  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();
  } 
  </code></pre>
  
  <p>How can I overwrite the text file and then display it in my TextBox on the same button click?</p>