C#读取txt文本的行数

 zhangy  2023-02-21 19:43:14  777  6  0

 直接代码:

  public static int readFileLines(string path)  //这里的参数是txt所在路径
        {
            int lines = 0;  //用来统计txt行数
            FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            StreamReader sr = new StreamReader(fs);
            while (sr.ReadLine() != null)
            {
                lines++;
            }

            fs.Close();
            sr.Close();

            return lines;

        }

调用方法:

  int fileTxTNumber = readFileLines(file.FullName);


作者禁止了评论
评论内容