我想从一个文本文件中读取所有的数据,但是使用 readline 只能读取一个回车符以前的字符,请教各位介绍合适的方法。谢谢。

解决方案 »

  1.   

    http://msdn.microsoft.com/zh-cn/library/system.io.file.readalllinesFile.ReadAllLines 方法
      

  2.   


                string path = @"E:\CollegeStudy\c# 资料\C__WCF入门学习.txt";
                StreamReader sr = new StreamReader(path, Encoding.GetEncoding("GB2312"));
                string text = sr.ReadToEnd();
                Console.WriteLine(text);
                sr.Close();
      

  3.   


    //点击打开按钮的事件
            private void miOpen_Click(object sender, EventArgs e)
            {
                OpenFileDialog dig=new OpenFileDialog();
                dig.ShowDialog();
                if (dig.FileName!="")
                {
                    SetTitle(dig.FileName);
                    filename = dig.FileName;
                    OpenFile(dig.FileName);//调用OpenFile方法
                }
                else
                {
                    MessageBox.Show("请选择文件!");
                }
               
            }        //打开文件的方法
            protected void OpenFile(string path)
          {
              try
              {
                  textBox1.Clear();
                  textBox1.Text = File.ReadAllText(path, UnicodeEncoding.GetEncoding("GB2312")); //文件的读取
              }
              catch(IOException e)
              {
                  MessageBox.Show(e.Message, "hhh",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
              }
          }