没有MSDN真痛苦
找个例子都找不到
劳烦各位给个读取TXT文件的示例代码
谢谢

解决方案 »

  1.   

    FileStream  fs  =  new  FileStream  (  "C:\\file.txt"    ,  FileMode.Open  ,  FileAccess.Read  )  ;
            StreamReader  m_streamReader  =  new  StreamReader  (  fs  )  ;  
        //使用StreamReader类来读取文件
        m_streamReader.BaseStream.Seek  (  0  ,  SeekOrigin.Begin  )  ;
            //  从数据流中读取每一行,直到文件的最后一行,并在richTextBox1中显示出内容
            this.richTextBox1.Text  =  ""  ;
            string  strLine  =  m_streamReader.ReadLine  (  )  ;
            while  (  strLine  !=  null  )
            {
                this.richTextBox1.Text  +=  strLine  +  "\n"  ;
                strLine  =  m_streamReader.ReadLine  (  )  ;
            }
            //关闭此StreamReader对象
            m_streamReader.Close  (  )  ;    
      

  2.   

    using System;
    using System.IO;
    using System.Text;class Test 
    {
        
        public static void Main() 
        {
            string path = @"c:\temp\MyTest.txt";        // Delete the file if it exists.
            if (File.Exists(path)) 
            {
                File.Delete(path);
            }        //Create the file.
            using (FileStream fs = File.Create(path)) 
            {
                AddText(fs, "This is some text");
                AddText(fs, "This is some more text,");
                AddText(fs, "\r\nand this is on a new line");
                AddText(fs, "\r\n\r\nThe following is a subset of characters:\r\n");            for (int i=1;i < 120;i++) 
                {
                    AddText(fs, Convert.ToChar(i).ToString());                //Split the output at every 10th character.
                    if (Math.IEEERemainder(Convert.ToDouble(i), 10) == 0) 
                    {
                        AddText(fs, "\r\n");
                    }
                }
            }        //Open the stream and read it back.
            using (FileStream fs = File.OpenRead(path)) 
            {
                byte[] b = new byte[1024];
                UTF8Encoding temp = new UTF8Encoding(true);
                while (fs.Read(b,0,b.Length) > 0) 
                {
                    Console.WriteLine(temp.GetString(b));
                }
            }
        }    private static void AddText(FileStream fs, string value) 
        {
            byte[] info = new UTF8Encoding(true).GetBytes(value);
            fs.Write(info, 0, info.Length);
        }
    }
    http://msdn.microsoft.com/en-us/library/system.io.filestream.aspx
      

  3.   

    filesteam fs=new xxx;
    streamreader sr=new streamreader(fs);
    sr.readline();
    sr.readxxx();
      

  4.   


    using(StreamReader sr = File.OpenText(filePath)
    {
       while(true)
       {
           string currentLine = sr.ReadLine();
           if(currentLine ==null)break;
           Console.WriteLine(currentLine);  
       }
    }
      

  5.   

    先写命名空间using System.io;
     if (!Directory.Exists(@"c:\temp"))
                {
                    Directory.CreateDirectory(@"c:\temp");
                }
                if(!File.Exists(@"c:\temp\1.txt"))
                {
                   FileStream fs = File.Create(@"c:\temp\1.txt");
                   fs.Close();
                    
                }
                OpenFileDialog openfile = new OpenFileDialog();
                openfile.Filter = "文本类型|*.txt|其他类型|*.*";
                if (openfile.ShowDialog() == DialogResult.OK) 
                {
                    this.txtPath.Text = openfile.FileName;
                }
                using (StreamReader Sr = new StreamReader(this.txtPath.Text)) 
                {
                    string Line;
                    Line = Sr.ReadToEnd();
                    this.txtContent.Text = Line;
                }
      

  6.   

    用那么麻烦吗?string fs = File.ReadAllText("C:\\a.txt");
      

  7.   

    string fs = System.IO.File.ReadAllText("C:\\a.txt");一句读完.如果txt不是巨大的话