一切福田,不離方寸,從心而覓,感無不通。

asp.net向文本文件txt末尾追加内容

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.IO;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string Path = Server.MapPath("1.txt");
string strings = "1232222";
FileAdd(Path, strings);

}

public static void FileAdd(string Path, string strings)
{
//实现一个System.IO.StreamWrite,使其以一种特定的编码向流中写入数据,AppendText数据追加到文件末尾
//这句也可以写成StreamWriter sw = new StreamWriter(Path, true, Encoding.GetEncoding("GB2312"));
StreamWriter sw = File.AppendText(Path);

//开始写入
sw.WriteLine(strings);

//清理当前编码器的缓冲区,并将所有缓存数据写入基础流
sw.Flush();
sw.Close();
}

}

转自:http://blog.sina.com.cn/s/blog_812ce0500100ywjb.html