开发者

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

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

I'm successfully displaying the text file data in a textbox. But while overwriting, it appends in the text file rather than overwriting.

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(开发者_JS百科"");
    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 can I overwrite the text file and then display it in my TextBox on the same button click?


StreamWriter constructor has several overloads, including one to specify whether to append or overwrite.

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

From MSDN, the second parameter:

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


Server.MapPath returns a string with the path to the file. You might try opening the file by hand before passing it to the stream writer. Note the FileMode of Create, and the 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
    }
}


Check out the System.IO.File.WriteAllText method, it'll overwrite the file if it exists and it's all done in just a single line of code. Likewise, 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);
} 


You appear to be overwriting the file in your Button Click handler, then appending it's contents to the TextBox. In this way, it appears to the client that you've appended to the file.

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

TextBox1.Text = "";

Or just don't bother reading it back - there's not much point because the text you've written to the file is still in the TextBox.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜