直接代码:
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);