使用换行符将文本转储到文件 [英] Dump text to file with line breaks

查看:103
本文介绍了使用换行符将文本转储到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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是一个Rich文本框,其中包含换行符(新行).

This dumps the text of txtChange to a text file. txtChange is a Rich text box and has line breaks (new lines) in it.

当用户单击转储"按钮时,所有文本都会被转储,但不会出现在新行上.

When the user clicks the Dump button all the text is Dumped but not on new lines.

例如txtChange看起来像

E.g. txtChange looks like

1
2
3
4

转储文本看起来像1234

如何设置文本转储的格式,以使文本位于新行上?

推荐答案

您应使用

You should use the Lines property instead:

File.WriteAllLines(@"E:\TestFile.txt", txtChange.Lines);

您真的不需要使用流,因为File类包含这些静态便捷方法-简而言之.

You don't really need to use a stream since the File class contains these static convenience methods - short and to the point.

以上内容将所有现有内容替换为文本框txtChange中包含的文本行.如果要附加内容,请改用适当命名的File.AppendAllLines().

Above will replace any existing content with the text lines contained in your text box txtChange. If you want to append content use the appropriately named File.AppendAllLines() instead.

这篇关于使用换行符将文本转储到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆