通过程序 VB.Net 或 C# 读取文本文件行数
1, VB.NET 读取 (通过streamReader)
public static string GetFileRowCount_Info(string sFileFullName) { if (!File.Exists(sFileFullName)) return ""; string output = ""; try { System.Diagnostics.Process myProcess = new System.Diagnostics.Process(); myProcess.StartInfo.FileName = "cmd.exe"; myProcess.StartInfo.UseShellExecute = false; myProcess.StartInfo.RedirectStandardInput = true; myProcess.StartInfo.RedirectStandardOutput = true; myProcess.Start(); StreamWriter myStreamWriter = myProcess.StandardInput; //myStreamWriter.WriteLine(sFileFullName.Substring(0, sFileFullName.IndexOf(":") + 1)); myStreamWriter.WriteLine("find /V \"\" /C " + @sFileFullName); myStreamWriter.Close(); output = myProcess.StandardOutput.ReadToEnd(); myProcess.WaitForExit(); } catch (Exception e) { Console.WriteLine(e); return ""; } return output; } public static long GetTxtRowCount(string sFileFullName) { if (!File.Exists(sFileFullName)) return -1; string sResult = GetFileRowCount_Info(sFileFullName); if (sResult == "") return -1; long lResult = 0; string[] lines = sResult.Split(System.Convert.ToChar("\n")); string sTmp = ""; foreach (string s in lines) { sTmp = s.TrimEnd(System.Convert.ToChar("\r")).ToUpper(); if (sTmp == "") continue; if (!sTmp.Contains(".TXT")) continue; // 不是.TXT的排除 if (!sTmp.StartsWith("----------")) continue; long.TryParse(sTmp.Split(':')[2].Trim(), out lResult); // 这里需要根据实际情况来 break; } return lResult; }
(结束)