我用C#做了个记事本,用richTextBox1.LoadFile()的方法打开windows的记事本显示是乱码,向各位高手请教该用什么打开方法可以实现?
如下是我写的代码,如何修改可以实现与windows的记事本兼容打开? private void 打开OToolStripButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileone = new OpenFileDialog();
            fileone.Filter = "文本文件(*.txt)|*.txt|RTF文件|*.rtf|所有文件(*.*)|*.*";
            fileone.FilterIndex = 1;
            if (fileone.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    this.richTextBox1.LoadFile(fileone.FileName);                }
                catch (Exception)
                {
                    MessageBox.Show("Oh,您的打开不成功!");
                }
            }
        }

解决方案 »

  1.   

    不是乱码吧。那是rtf文件的原因。。打开方式不正确            OpenFileDialog openfiledialog = new OpenFileDialog();
                openfiledialog.InitialDirectory = "c:\\";
                openfiledialog.Filter = "文本文件|*.txt|RTF文件|*.rtf|所有文件|*.*";
                openfiledialog.FilterIndex = 1;
                openfiledialog.RestoreDirectory = false;
                if (openfiledialog.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        string filename = openfiledialog.FileName;
                        childForm newform = new childForm();
                        newform.MdiParent = this;
                        string fileext = new System.IO.FileInfo(filename).Extension;
                        switch (fileext.ToLower())
                        {                        case ".txt":
                                newform.richTextBox1.LoadFile(filename, RichTextBoxStreamType.PlainText);
                                break;
                            case ".rtf":
                                newform.richTextBox1.LoadFile(filename, RichTextBoxStreamType.RichText);
                                break;
                        }
                        newform.Text = System.IO.Path.GetFileNameWithoutExtension(filename) + "--简易写字板";
                        newform.SaveFileName = filename;
                        newform.Show();
                        this.toolStripMain.Enabled = true;
                        this.toolStripFormat.Enabled = true;
                        newform.SetTools += new childForm.SetToolsEventHandler(SetTools);
                    }
                    catch
                    {
                        MessageBox.Show("文件格式不正确", "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
      

  2.   

    打开windows的记事本是显示错误,用windows记事本打开c#的记事本是显示乱码,childform 是要using哪个命名空间?
      

  3.   

    会不会是编码问题?
    那些文本可能会被人调过编码格式,判断一下是UTF-8或是GB2312的,读的时候加上编码应该就可以正常显示了.
      

  4.   

    5楼如果你的编辑器可以与windows的记事本兼容使用,发给我看看,先谢了!
      

  5.   

    private void 打开OToolStripButton_Click(object sender, EventArgs e) 
            { 
                OpenFileDialog fileone = new OpenFileDialog(); 
                fileone.Filter = "文本文件(*.txt)|*.txt|RTF文件|*.rtf|所有文件(*.*)|*.*"; 
                fileone.FilterIndex = 1; 
                if (fileone.ShowDialog() == DialogResult.OK) 
                { 
                    try 
                    { 
                         string filename = openfiledialog.FileName; 
                        string fileext = new System.IO.FileInfo(filename).Extension; 
                        switch (fileext.ToLower()) 
                        {                         case ".txt": 
                                newform.richTextBox1.LoadFile(filename, RichTextBoxStreamType.PlainText); 
                                break; 
                            case ".rtf": 
                                newform.richTextBox1.LoadFile(filename, RichTextBoxStreamType.RichText); 
                                break; 
                        } 
                    } 
                    catch (Exception) 
                    { 
                        MessageBox.Show("Oh,您的打开不成功!"); 
                    } 
                } 
            }这样试过了没有?
      

  6.   

    newform.richtextbox1换成你自己的form名加richtextbox的Name
      

  7.   

    “string filename = openfiledialog.FileName;”这样定义是不正确的! 我试过下面的代码 ,不论是c#的记事本 还是windows的记事本都打不开!
    private void 打开OToolStripButton_Click(object sender, EventArgs e)
            {
                OpenFileDialog fileone = new OpenFileDialog();
                fileone.Filter = "文本文件(*.txt)|*.txt|RTF文件|*.rtf|所有文件(*.*)|*.*";
                fileone.FilterIndex = 1;
                if (fileone.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        OpenFileDialog file =new OpenFileDialog();
                        string filename = file.FileName;
                        string fileext = new System.IO.FileInfo(filename).Extension;
                        switch (fileext.ToLower())
                        {                        case ".txt":
                                this.richTextBox1.LoadFile(filename, RichTextBoxStreamType.PlainText);
                                break;
                            case ".rtf":
                                this.richTextBox1.LoadFile(filename, RichTextBoxStreamType.RichText);
                                break;
                        }
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Oh,您的打开不成功!");
                    }
                } 
      

  8.   

    http://download.csdn.net/source/937260,这些都是我搜集的关于使用richtextbox做记事本的,有的很不错!我的是根据这些代码修改的!
      

  9.   

    把this.richTextBox1.LoadFile(fileone.FileName); 
    改成this.richTextBox1.LoadFile(fileone.FileName,RichTextBoxStreamType.PlainText); 
      

  10.   

    用13楼的方法“把this.richTextBox1.LoadFile(fileone.FileName); 
                 改成this.richTextBox1.LoadFile(fileone.FileName,RichTextBoxStreamType.PlainText);”
    可以打开windows的记事本,但是打开c#记事本时就是乱码了,这里如何解决呢?