从 RichTextBox 到文本文件,逐行

From RichTextBox to text files, line after line(从 RichTextBox 到文本文件,逐行)

本文介绍了从 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 到文本文件,逐行

基础教程推荐