gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 17:15:37 26 4
gpt4 key购买 nike

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

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

最佳答案

你应该使用 Lines属性代替:

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

您实际上不需要使用流,因为 File 类包含这些静态便捷方法 - 简明扼要。

上面将用文本框 txtChange 中包含的文本行替换任何现有内容。如果您想附加内容,请改用适当命名的File.AppendAllLines()

关于c# - 使用换行符将文本转储到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8327014/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com