在丰富的文本框中打开文件用C# [英] Open file in rich text box with C#

查看:152
本文介绍了在丰富的文本框中打开文件用C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题就有了答案。我建议以下sumit_programmers解决方案。现在,我已删除了我的代码,认为它是困惑多于帮助。当我已经开发了它远一点,也许我会在这里发布我的代码,有一些评论。



您还可能有兴趣在回答这个问题< A HREF =htt​​p://stackoverflow.com/questions/3751929/save-text-from-rich-text-box-with-c>保存用C#从丰富的文本框的文本。有接受的答案的提醒对这一问题的答案。该代码应工作,但它是我写的,所以可能会有一些错误或遗漏的信息






更​​新:我提高了代码位(至少我是这么认为的)。 Encoding.Default似乎与大多数常见的编码工作,如ANSI。如果编码为UTF-8没有字节顺序标记(BOM),似乎Encoding.Default不工作,虽然。欲了解更多信息,请访问 informit.com/guides 。以下是我现在使用的代码:

 私人无效fileOpen_Click(对象发件人,EventArgs五)
{
按(打开文件对话框dlgOpen =新的OpenFileDialog())
{

{
//可用的文件扩展名
dlgOpen.Filter =所有文件(*。 。*)| *。*。
//初始目录
dlgOpen.InitialDirectory =D:;
//打开文件对话框的标题
dlgOpen.Title =打开;
//显示打开文件对话框框
如果(dlgOpen.ShowDialog()== DialogResult.OK)
{
//创建一个新的StreamReader
StreamReader的SR =新的StreamReader( dlgOpen.FileName,Encoding.Default);
//从文件
字符串str = sr.ReadToEnd所有文本();
//关闭StreamReader的
sr.Close();
//显示在富文本rtbMain
rtbMain.Text = str中的文本;
}
}
赶上(例外ERRORMSG)
{
MessageBox.Show(errorMsg.Message);
}
}
}


解决方案

是的,你得到的错误与您试图访问无法在格式文本框加载文件。如果你想加载一个.rtf文件,你需要加入这行

  richTextBox1.LoadFile(dlg.FileName,RichTextBoxStreamType.RichText ); 



如果要加载.txt文件,你需要添加此



  richTextBox1.LoadFile(dlg.FileName,RichTextBoxStreamType.PlainText); 



示例代码:



 使用(打开文件对话框OFD =新的OpenFileDialog())
{

{
ofd.Filter =文本文件( * .TXT)| * .TXT | RTF文件(* .RTF)| * .RTF
如果(ofd.ShowDialog()== DialogResult.OK)
{
如果(Path.GetExtension(ofd.FileName)==的.rtf)
{
richTextBox1.LoadFile(ofd.FileName,RichTextBoxStreamType.RichText);
}
如果(Path.GetExtension(ofd.FileName)==名.txt)
{
richTextBox1.LoadFile(ofd.FileName,RichTextBoxStreamType.PlainText);
}

}
}
赶上(异常前)
{
}
}


This question have been answered. I recommend sumit_programmers solution below. For now, I've removed my code, thinking it's more confusing than helpful. When I've developed it a bit further, perhaps I'll post my code here, with some comments.

You may also be interested in the answer to the question Save text from rich text box with C#. There is an answer that reminds of the accepted answer to this question. The code should work, but it's written by me, so there may be some errors or missing information.


Update: I have improved the code a bit (at least I think so). "Encoding.Default" seems to work with most common encodings, like ANSI. If the encoding is UTF-8 without byte order mark (BOM), it seems "Encoding.Default" doesn't work, though. For more information, go to informit.com/guides. Here's the code I'm using right now:

private void fileOpen_Click(object sender, EventArgs e)
{
  using (OpenFileDialog dlgOpen = new OpenFileDialog())
  {
    try
    {
      // Available file extensions
      dlgOpen.Filter = "All files(*.*)|*.*";
      // Initial directory
      dlgOpen.InitialDirectory = "D:";
      // OpenFileDialog title
      dlgOpen.Title = "Open";
      // Show OpenFileDialog box
      if (dlgOpen.ShowDialog() == DialogResult.OK)
      {
        // Create new StreamReader
        StreamReader sr = new StreamReader(dlgOpen.FileName, Encoding.Default);
        // Get all text from the file
        string str = sr.ReadToEnd();
        // Close the StreamReader
        sr.Close();
        // Show the text in the rich textbox rtbMain
        rtbMain.Text = str;
      }
    }
    catch (Exception errorMsg)
    {
      MessageBox.Show(errorMsg.Message);
    }
  }
}

解决方案

Yes, you are getting that error as you are trying to access file that can't be loaded in Rich Text Box. If you want to load a .rtf file you need to add this line

richTextBox1.LoadFile(dlg.FileName, RichTextBoxStreamType.RichText);

and if you want to load .txt file, you need to add this

richTextBox1.LoadFile(dlg.FileName, RichTextBoxStreamType.PlainText);

Sample Code:

 using (OpenFileDialog ofd = new OpenFileDialog())
        {
            try
            {
                ofd.Filter = "Text files (*.txt)|*.txt|RTF files (*.rtf)|*.rtf";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    if (Path.GetExtension(ofd.FileName) == ".rtf")
                    {
                        richTextBox1.LoadFile(ofd.FileName, RichTextBoxStreamType.RichText);
                    }
                    if (Path.GetExtension(ofd.FileName) == ".txt")
                    {
                        richTextBox1.LoadFile(ofd.FileName, RichTextBoxStreamType.PlainText);
                    }

                }
            }
            catch (Exception ex)
            {
            }
        }

这篇关于在丰富的文本框中打开文件用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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