FileStream fs = new FileStream("E:\Test\HistoryData.txt", FileMode.Append, FileAccess.Write);
StreamWriter sw1 = new StreamWriter(fs);
listboxdata = textBox1.Text + "
";
//获取文件的大小,文件过大则删除之前的内容
string filePath = "E:\Test\HistoryData.txt";
FileInfo fileInfo = new FileInfo(filePath);
double FileLength = fileInfo.Length;//读取文件大小(字节数)
double FileLenKB = 0.0;
if (FileLength > 1024)//将文件大小转换为KB
{
FileLenKB = FileLength / 1024;
if (FileLenKB > 50000)
{
sw1.Close();
fs.Close();
fs = new FileStream("E:\Test\HistoryData.txt", FileMode.Truncate, FileAccess.ReadWrite);//清空文件内容
fs.Close();
fs = new FileStream("E:\Test\HistoryData.txt", FileMode.Append, FileAccess.Write);//重新打开文件
sw1 = new StreamWriter(fs);
}
}
//获取文件的大小,文件过大则删除之前的内容
sw1.Write(listboxdata + "
");//开始写入值
sw1.Close();
fs.Close();