从RichTextBox的行之后到文本文件,行 [英] From RichTextBox to text files, line after line

查看:178
本文介绍了从RichTextBox的行之后到文本文件,行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有从RichTextBox的文件保存为文本文件的麻烦。



我的RichTextBox看起来像这样;

  ABC ... 
SDE ...
KLO ...

在我保存它看起来是这样的:

  ABC ... SDE ... ... KLO 

但我想要的行之后同样喜欢的RichTextBox线。我做了什么错了?



 如果(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();



我们推荐使用 sw.WriteLine() richTextBox1 然后移动到另一行。



示例

 的for(int i = 0; I< richTextBox1.Lines.Length;我++)
{
sw.WriteLine(richTextBox1.Lines [I]);
}
sw.Flush();
sw.Close();



另一个解决方案






还有已经为的RichTextBox 内置功能来保存文件使用特定编码。您可以使用 RichTextBox.SaveFile()用于这一目的。



示例

  RichTextBox.SaveFile(字符串路径,RichTextBoxStreamType); 



其中,路径表示 saveFileDialog2.FileName 在你的代码。对于 RichTextBoxStreamType ,最好将其设置为 RichTextBoxStreamType.PlainText 只要你不使用RTF,如颜色/字体/保护/缩进的/ etc ...



然后,您可以读取文件再次使用下面的方法。

  RichTextBox.LoadFile(字符串路径,RichTextBoxStreamType); 



注意:如果该文件不在RTF,并尝试阅读它RTF( RichTextBox.LoadFile(字符串路径,RichTextBoxStreamType.RichText);
你可能会遇到格式错误。在这种情况下,你需要捕获异常,并读入平原或Unicode编码的文件。



示例



 的RichTextBox _RichTextBox =新的RichTextBox(); 

{
_RichTextBox.LoadFile(@D:\Resources\text.txt,RichTextBoxStreamType.RichText);
}
赶上(例外EX)
{
如果(EX.Message.ToLower()。包含(格式不正确))
{
_RichTextBox.LoadFile(@D:\Resources\text.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\text.txt", RichTextBoxStreamType.RichText);
}
catch (Exception EX)
{
     if (EX.Message.ToLower().Contains("format is not valid"))
     {
          _RichTextBox.LoadFile(@"D:\Resources\text.txt", RichTextBoxStreamType.PlainText);
     }
}

Thanks,
I hope you find this helpful :)

这篇关于从RichTextBox的行之后到文本文件,行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆