开发者

appending info to a text file

I have a web site with which I am adding records to a database and The info I am also writing to a log file which is part of the website. The info is written to the file fine...the only question I do have is how to add information with each input into the database. As it is at the moment the information is put int the lof file but with each new insert into the database the existing record in the log file seems to be overwritten and replaced with a new insert so that there is al开发者_如何学运维ways only one record added to the log file. Advice perhaps... this is the code I used in the btnClick event.

protected void btnSubmitRating_Click1(object sender, EventArgs e)
{
    int rating = 0;
    try
    {
        if (radRating.Items[0].Selected)
        {
            rating = 1;
        }
        if (radRating.Items[1].Selected)
        {
            rating = 2;
        }
        else if (radRating.Items[2].Selected)
        {
            rating = 3;
        }
        else if (radRating.Items[3].Selected)
        {
            rating = 4;
        }
        else if (radRating.Items[4].Selected)
        {
            rating = 5;
        }
        FileStream rate = new FileStream(Server.MapPath("~/rateBooks.log"), FileMode.Open, FileAccess.ReadWrite);
        BufferedStream bs = new BufferedStream(rate);
        rate.Close();


        StreamWriter sw = new StreamWriter(Server.MapPath("~/rateBooks.log"));
        sw.WriteLine("userName " + txtUserName.Text + " " + "bookTitle " + txtBookTitle.Text + " " + "ISBN " + txtISBN.Text);
        sw.Close();


        Service s = new Service();
        s.bookRatedAdd(txtBookTitle.Text, rating, txtReview.Text, txtISBN.Text, txtUserName.Text);
        btnSubmitRating.Enabled = false;
        radRating.Enabled = false;            
    }
    catch (Exception em)
    {
        lblError.Text = em.Message; 
        //lblError.Text = "This book has already been reviewed by yourself?";
    }
}

Kind regards


Change this whole block of code:

FileStream rate = new FileStream(Server.MapPath("~/rateBooks.log"), FileMode.Open, FileAccess.ReadWrite);
BufferedStream bs = new BufferedStream(rate);
rate.Close();

StreamWriter sw = new StreamWriter(Server.MapPath("~/rateBooks.log"));
sw.WriteLine("userName " + txtUserName.Text + " " + "bookTitle " + txtBookTitle.Text + " " + "ISBN " + txtISBN.Text);
sw.Close();

To this single line:

File.AppendAllText(Server.MapPath("~/rateBooks.log"), "userName " + txtUserName.Text + " " + "bookTitle " + txtBookTitle.Text + " " + "ISBN " + txtISBN.Text + Environment.NewLine);

Note that I'm now adding manually NewLine to each line.


Read this: How to: Write to a Text File (C# Programming Guide)

In that, example #4 shows how to append to a file:

// Example #4: Append new text to an existing file
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Public\TestFolder\WriteLines2.txt", true))
{
  file.WriteLine("Fourth line");
}

So you could do this with your code and it should work:

StreamWriter sw = new StreamWriter(Server.MapPath("~/rateBooks.log"), true);
sw.WriteLine("userName " + txtUserName.Text + " " + "bookTitle " + txtBookTitle.Text + " " + "ISBN " + txtISBN.Text);
sw.Close();

EDIT: Just use Shadow Wizard's answer, it is much cleaner...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜