建立一个简单的记事本程序中怎么从硬盘中取出文件

解决方案 »

  1.   

    使用 FileStream 从文本文件中读取文件,放入文本框。最好用 OpenFileDialog 让用户选择要打开的文件。
      

  2.   

    必须先拖一个OpenFileDialog 控件到窗体上,即显示一个对话框,用于打开用户文件。然后具体实现功能的代码如下:   open_menu是(打开)按钮,如下是它的单击事件 private void open_menu_Click(object sender, EventArgs e)
     {
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
           string fName = openFileDialog1.FileName;
           string ext = Path.GetExtension(fName);
           richTextBox1.Clear();
           try
           {
              if (ext == ".txt")
              {
                richTextBox1.LoadFile(fName, RichTextBoxStreamType.PlainText);
              }
              else
              {
                richTextBox1.LoadFile(fName, RichTextBoxStreamType.RichText);
              }
            }
            catch (ArgumentException exception)
            {
                    MessageBox.Show("无法识别的文件类型","打开出   错",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }
     }这样就能打开用户硬盘上的文本文件了。
      

  3.   

    不拖动OpenFileDialog 也可以
    定义一个OpenFileDialog 类对象直接调用对象的ShowDialog方法。