From RichTextBox to text files, line after line(从 RichTextBox 到文本文件,逐行)
问题描述
我无法将文件从 Richtextbox 保存到文本文件.
我的富文本框是这样的;
ABC ...SDE ...克洛...
保存后是这样的:
ABC ... SDE ... KLO ...
但我想要一行又一行的 Richtextbox 一样.我做错了什么?
if (saveFileDialog2.ShowDialog() == DialogResult.OK){StreamWriter sw = File.CreateText(saveFileDialog2.FileName);sw.WriteLine(richTextBox1.Text);sw.Flush();sw.Close();//File.WriteAllText(saveFileDialog2.FileName, str);}
你可能会得到这个,因为你试图将 richTextBox1.Text
(整个文本)保存在一行中,只使用以下内容代码
StreamWriter sw = File.CreateText(saveFileDialog2.FileName);sw.WriteLine(richTextBox1.Text);sw.Flush();sw.Close();
建议在richTextBox1
中的特定行号上使用sw.WriteLine()
,然后移动到另一行.
示例
for (int i = 0; i
另一种解决方案
<小时>RichTextBox
已经有一个内置函数来保存具有特定编码的文件.为此,您可以使用 RichTextBox.SaveFile()
.
示例
RichTextBox.SaveFile(字符串路径, RichTextBoxStreamType);
path
代表代码中的 saveFileDialog2.FileName
.对于RichTextBoxStreamType
,最好设置成RichTextBoxStreamType.PlainText
,只要不使用Color/Font/Protection/Indent/等RTF...p>
然后,您可以使用以下方法再次读取文件
RichTextBox.LoadFile(字符串路径, RichTextBoxStreamType);
注意:如果文件不在 RTF 中并且您尝试在 RTF 中读取它(RichTextBox.LoadFile(string path, RichTextBoxStreamType.RichText);
)您可能会遇到格式错误.在这种情况下,您需要捕获异常并以普通或 Unicode 编码读取文件.
示例
RichTextBox _RichTextBox = new RichTextBox();尝试{_RichTextBox.LoadFile(@"D:Resources ext.txt", RichTextBoxStreamType.RichText);}捕获(异常 EX){if (EX.Message.ToLower().Contains("格式无效")){_RichTextBox.LoadFile(@"D:Resources ext.txt", RichTextBoxStreamType.PlainText);}}
谢谢,
希望对您有所帮助:)
I have trouble with saving file from Richtextbox to text file.
My richtextbox looks like this;
ABC ...
SDE ...
KLO ...
After i saved it looks like this:
ABC ... SDE ... KLO ...
But i want the same like richtextbox line after lines. What did i do wrong?
if (saveFileDialog2.ShowDialog() == DialogResult.OK)
{
StreamWriter sw = File.CreateText(saveFileDialog2.FileName);
sw.WriteLine(richTextBox1.Text);
sw.Flush();
sw.Close();
//File.WriteAllText(saveFileDialog2.FileName, str);
}
You are probably getting this because you are trying to save richTextBox1.Text
(the whole text) in one line only using the following code
StreamWriter sw = File.CreateText(saveFileDialog2.FileName);
sw.WriteLine(richTextBox1.Text);
sw.Flush();
sw.Close();
It's recommended to use sw.WriteLine()
on a specific line number in richTextBox1
then move to another line.
Example
for (int i = 0; i < richTextBox1.Lines.Length; i++)
{
sw.WriteLine(richTextBox1.Lines[i]);
}
sw.Flush();
sw.Close();
Another Solution
There's already a built-in function for RichTextBox
to save a file with a specific encoding. You may use RichTextBox.SaveFile()
for this purpose.
Example
RichTextBox.SaveFile(string path, RichTextBoxStreamType);
Where path
represents saveFileDialog2.FileName
in your code. For RichTextBoxStreamType
, it's best to set it as RichTextBoxStreamType.PlainText
as long as you do not use RTF such as Color/Font/Protection/Indent/etc...
Then, you may read the file again using the following method
RichTextBox.LoadFile(string path, RichTextBoxStreamType);
NOTICE: If the file is not in RTF and you try to read it in RTF (RichTextBox.LoadFile(string path, RichTextBoxStreamType.RichText);
)
you may encounter formatting errors. In this case, you'll need to catch the exception and read the file in a Plain or Unicode encoding.
Example
RichTextBox _RichTextBox = new RichTextBox();
try
{
_RichTextBox.LoadFile(@"D:Resources ext.txt", RichTextBoxStreamType.RichText);
}
catch (Exception EX)
{
if (EX.Message.ToLower().Contains("format is not valid"))
{
_RichTextBox.LoadFile(@"D:Resources ext.txt", RichTextBoxStreamType.PlainText);
}
}
Thanks,
I hope you find this helpful :)
这篇关于从 RichTextBox 到文本文件,逐行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 RichTextBox 到文本文件,逐行
基础教程推荐
- 将 XML 转换为通用列表 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- c# Math.Sqrt 实现 2022-01-01