winform中,读取txt文件,并显示在textbox中,遇到2个问题,
1无法显示汉字
2不能完全显示,只能显示部分,我设了多行显示,把textBox2.MaxLength = 99999也不行,也有滚动条,
是不是textBox有默认的最大限制,那我用什么显示比较好?listbox我试了也不行,richtextbox也不行
下面是代码
 string path;
          path= Path.GetDirectoryName(Application.ExecutablePath) + "\\"+ dataGridView1.CurrentRow.Cells[4].Value .ToString ();
          string stype = path.Substring(path.LastIndexOf("."));//上面是路径,没有问题
          if (stype == ".txt")
          {
              if (!File.Exists(path))
              {
                  MessageBox.Show("文件不存在或已经删除");
              }
                else
              {
                  FileStream fs = File.OpenRead(path);
                  byte[] arr = new byte[1000];
                  UTF8Encoding data = new UTF8Encoding(true);                  while (fs.Read(arr, 0, arr.Length) > 0)
                  {
                      textBox2.Text = data.GetString(arr);
                  }
              } 
           }         
          else
          {
              MessageBox.Show("只能阅读.txt文件,其他文件请下载");
          }

解决方案 »

  1.   

    trystring path;
              path= Path.GetDirectoryName(Application.ExecutablePath) + "\\"+ dataGridView1.CurrentRow.Cells[4].Value .ToString ();
              string stype = path.Substring(path.LastIndexOf("."));//上面是路径,没有问题
              if (stype == ".txt")
              {
                  if (!File.Exists(path))
                  {
                      MessageBox.Show("文件不存在或已经删除");
                  }
                    else
                  {
                      FileStream fs = File.OpenRead(path);
                      StreamReader sr = new StreamReader(fs,Encoding.Default);
                      textBox2.Text = sr.ReadToEnd();
                  }
              }       
              else
              {
                  MessageBox.Show("只能阅读.txt文件,其他文件请下载");
              }
      

  2.   

    首先 byte[] arr = new byte[1000]; 
    如果是UTF-8  采用1000的byte是不行的  UTF-8汉字长度是3另外textBox2.MaxLength = 99999不知道是什么意思,
    写软件的看到99999  应该想到要么报错,要么长度不为最大另外读数据改成
    byte[] arr = new byte[ 4096 ];
    int read = fs.Read( arr, 0, arr.Length );
    while ( read > 0 )
    {
         textBox2.Text = data.GetString( arr,0,read );
         read = fs.Read( arr, 0, arr.Length );
    }string stype = path.Substring(path.LastIndexOf("."));
    改成System.IO.Path.GetExtension( path ).ToLower();
      

  3.   

    FileStream fs = File.OpenRead(path); 
    byte[] arr = new byte[1000]; 
    UTF8Encoding data = new UTF8Encoding(true); while (fs.Read(arr, 0, arr.Length) > 0) 

        textBox2.Text = data.GetString(arr); 

    ---------》
    using(StreamReader sr = new StreamReader(path,Encoding.Default))//这里的Encoding是你文本的编码
    {
        textBox2.Text = sr.ReadToEnd();
    }
      

  4.   

    对了  倒不是说用4096就可以  因为 里面可能有数字字母  你无法知道那儿截断了  只是使用read时候一般默认采用4096好点  至少是2^N
      

  5.   

    另外读数据改成 
    byte[] arr = new byte[ 4096 ]; 
    int read = fs.Read( arr, 0, arr.Length ); 
    while ( read > 0 ) 

        textBox2.Text = data.GetString( arr,0,read ); 
        read = fs.Read( arr,…
    [/Quote] textBox2.Text = data.GetString( arr,0,read ); 
    这个data是哪来的啊?
      

  6.   

    1楼和3楼的都可以实现,谢谢了,2楼的也采用了得到path扩展名