C# 中如何设置streamwriter 写入文件的格式是一行接一行的?
各位大神请指点一下~~~ 展开
private void button3_Click(object sender, EventArgs e)
{
this.saveFileDialog2.Filter = "保存文本文件.txt|*.txt|保存Excel文件.xls|*.xls";
if (saveFileDialog2.ShowDialog() == DialogResult.OK)
{
FileStream fileStr = new FileStream(this.saveFileDialog2.FileName, FileMode.Append);
StreamWriter strWri = new StreamWriter(fileStr, Encoding.GetEncoding("GB2312"));
strWri.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
strWri.WriteLine(this.textBox1.Text + "\t" + this.textBox2.Text);
strWri.WriteLine(this.textBox3.Text + "\t" + this.textBox4.Text);
strWri.Close();
fileStr.Close();
}
}
不存在log.txt就创建,存在就在后面追加:
using (System.IO.StreamWriter sw = System.IO.File.AppendText("D:\\log.txt")) {
sw.WriteLine("输出结果");
}