private void btnDump_Click(object sender, EventArgs e)
{
using (StreamWriter sw = new StreamWriter("E:\\TestFile.txt"))
{
// Add some text to the file.
sw.WriteLine(txtChange.Text);
}
}这会将txtChange的文本转储到文本文件中。txtChange是一个富文本框,其中包含换行符(新行)。
当用户单击转储按钮时,所有文本都会被转储,但不会在新行上转储。
例如,txtChange看起来像
1
2
3
4转储文本看起来像1234
如何格式化文本的转储以使文本位于新行上?
发布于 2011-11-30 13:55:51
您应该使用Lines属性来代替:
File.WriteAllLines(@"E:\TestFile.txt", txtChange.Lines);您实际上不需要使用流,因为File类包含这些静态方便的方法-简单明了。
上面将用文本框txtChange中包含的文本行替换任何现有内容。如果要附加内容,请使用适当的名称File.AppendAllLines()。
发布于 2011-11-30 13:57:02
只需添加一个换行符:
private void btnDump_Click(object sender, EventArgs e)
{
using (StreamWriter sw = new StreamWriter("E:\\TestFile.txt"))
{
// Add some text to the file.
sw.WriteLine(txtChange.Text + "\r\n");
}
}发布于 2011-11-30 13:57:21
如果如您所提到的那样包含\r's,则应尝试以下方法
using (StreamWriter sw = new StreamWriter("E:\\TestFile.txt"))
{
// Add some text to the file.
sw.WriteLine(txtChange.Text.Replace("\r", "\r\n");
}https://stackoverflow.com/questions/8327014
复制相似问题