private void tsmi_Open_Click(object sender, EventArgs e)
        {
            Stream myStream;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = "c:\\" ;
            openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2 ;
            openFileDialog1.RestoreDirectory = true ;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = openFileDialog1.OpenFile()) != null)
                {
                    byte[] bytes = new byte[myStream.Length];
                    int numBytesToRead = (int)myStream.Length;
                    string read = myStream.Read(bytes, 0, numBytesToRead).ToString();
                    RichTextBox1.Text = read.ToString();
                    myStream.Close();
                }
            }
        }Stream.Read读不出内容 读出了长度?  输出为 6 为什么?

解决方案 »

  1.   

    string read = myStream.Read(bytes, 0, numBytesToRead).ToString(); 
    这是获取了读到的字符长度,你读的东西在bytes里
    myStream.Read(bytes, 0, numBytesToRead);
    string read = System.Test.Encoding.Default.GetString(bytes);
      

  2.   

     Stream myStream; 
                OpenFileDialog openFileDialog1 = new OpenFileDialog(); 
                openFileDialog1.InitialDirectory = "c:\\" ; 
                openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; 
                openFileDialog1.FilterIndex = 2 ; 
                openFileDialog1.RestoreDirectory = true ; 
                if (openFileDialog1.ShowDialog() == DialogResult.OK) 
                { 
                    if ((myStream = openFileDialog1.OpenFile()) != null) 
                    { 
    StreamReader reader = new StreamReader(myStream);
                        
                        RichTextBox1.Text = reader.ReadToEnd();
                        myStream.Close(); 
    reader.Close();
                    } 
                }