怎么把文件里面的东西输入到我的Text里面去??

解决方案 »

  1.   

    MSDN上的例子        try 
            {
                // Create an instance of StreamReader to read from a file.
                // The using statement also closes the StreamReader.
                using (StreamReader sr = new StreamReader("TestFile.txt")) 
                {
                    String line;
                    // Read and display lines from the file until the end of 
                    // the file is reached.
                    while ((line = sr.ReadLine()) != null) 
                    {
                        Console.WriteLine(line);
                    }
                }
            }
            catch (Exception e) 
            {
                // Let the user know what went wrong.
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
      

  2.   

    改造一下this.richTextBox1.Text = String.Empty;
            try 
            {
                // Create an instance of StreamReader to read from a file.
                // The using statement also closes the StreamReader.
                using (StreamReader sr = new StreamReader("TestFile.txt")) 
                {
                    String line;
                    // Read and display lines from the file until the end of 
                    // the file is reached.
                    while ((line = sr.ReadLine()) != null) 
                    {
                        this.richTextBox1.Text.Append(line);
                    }
                }
            }
            catch (Exception e) 
            {
                // Let the user know what went wrong.
                MessageBox.Show("The file could not be read:" + e.Message);
            }
      

  3.   

    兄弟你有中文版的MSDN吗??给各地址  可以吗??
      

  4.   

    我没有机器上的一切软件都是英文和韩文的,不好意思那我帮你翻译一下this.richTextBox1.Text = String.Empty;
            try 
            {            // 创建一个StreamReader的实例,用它来读取一个文件。
                // using语句会自动关闭这个StreamReader(具体为什么就去看看MSDN吧,说得很清楚的,IDisposable).
                using (StreamReader sr = new StreamReader("TestFile.txt")) 
                {
                    String line;
                    // 一行一行的读取文件并显示(到richtextbox中)出来,直到文件被读取完毕.
                    while ((line = sr.ReadLine()) != null) 
                    {
                        this.richTextBox1.Text.Append(line);
                    }
                }
            }
            catch (Exception e) 
            {
                // 让用户知道发生了什么错误.
                MessageBox.Show("The file could not be read:" + e.Message);
            }
      

  5.   

    出错哦  Append好像不正确哦。。调试。
      

  6.   

    不好意思,写错了
    this.richTextBox1.Text.Append(line);改成this.richTextBox1.AppendText(line);
      

  7.   

    public string ReadFile(string fn)
        {
          string s = "";
          try
          {
            StreamReader sr = new StreamReader(fn, Encoding.GetEncoding("GB18030"));
            s = sr.ReadToEnd();
            sr.Close();
          }
          catch { }
          return s;
        }