使用换行符将文本转储到文件

编程入门 行业动态 更新时间:2024-10-27 16:23:39
本文介绍了使用换行符将文本转储到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 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

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

推荐答案

您应使用 Lines 属性改为:

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.

更多推荐

使用换行符将文本转储到文件

本文发布于:2023-10-15 01:28:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1492835.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本   文件   换行符   转储到

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!