0

I'm trying to get my streamwriter to add text onto the end of the text file but for some reason, it is just replacing the text with whatever I have entered. Any help is appreciated to fix this.

StreamWriter sw = new StreamWriter(Server.MapPath("~") + "/App_Data/blogMessages.txt");
sw.WriteLine(postTextBox.Text);
sw.Close();
4

3 回答 3

2

您可以对流编写器使用覆盖的方法

StreamWriter sw = new StreamWriter(Server.MapPath("~") + "/App_Data/blogMessages.txt", true);

这将附加您的文本

于 2013-05-30T12:24:03.217 回答
1

您需要使用带有额外参数的StreamWriter 构造函数的重载版本来配置覆盖或附加到流。添加 using 语句

using(StreamWriter sw = new StreamWriter(Server.MapPath("~") + 
                        "/App_Data/blogMessages.txt", true))
{

     sw.WriteLine(postTextBox.Text);

}

using 语句允许正确关闭和处理流,以防您在打开或写入时遇到异常

于 2013-05-30T12:23:52.963 回答
0

尝试这个

 using (System.IO.TextWriter tw = new System.IO.StreamWriter(@"D:\TEMP\IEALog\path.txt", true))
        {
            tw.WriteLine(message + "Source ;- " + source + "StackTrace:- " + exc.StackTrace.ToString());
        }
于 2013-05-30T12:24:39.777 回答