1.有一个txt文件 我想要得到里面的行数 (该txt文件有超过百万行文字,注意效率)StreamReader sr = new StreamReader(ofdJoin.FileName, System.Text.Encoding.Default);
                    string str = null;
                    int s = 0;
                    for (; ; )
                    {
                        str = sr.ReadLine();
                        if (str != null)
                        {
                            s += 1;
                        }
                        else
                        {
                            break;
                        }
                    }
这个是我之前写的 效率很低有没有别的方法 最好是.net封装好的可以直接调用的有没?2.另需要得到该txt文件的第i行的文字的办法。

解决方案 »

  1.   


    string[] lines = System.IO.File.ReadAllLines("路径");
    这是.net中已经封装好的方法,但效率估计不会高到哪里去
      

  2.   

     读写文本用ReadAllLines和WriteAllLines两个方法
     String[] lines = File.ReadAllLines(@".\config.txt");
    for (int i = 0; i < lines.Length; i++)
           Console.WriteLine(lines[i]);
      

  3.   

    String[] lines=System.IO.File.ReadAllLines("路径");
    .另需要得到该txt文件的第i行的文字的办法。
     第i行就是lines[i]