Dump text to file with line breaks(将文本转储到带有换行符的文件)
问题描述
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 是一个富文本框,其中包含换行符(新行).
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.
例如txt更改看起来像
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.
Above 将使用文本框 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.
这篇关于将文本转储到带有换行符的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将文本转储到带有换行符的文件
基础教程推荐
- Moq It.Is<>不匹配 2022-01-01
- WPF 模态进度窗口 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
