通过程序 VB.Net 或 C# 读取文本文件行数

前端之家收集整理的这篇文章主要介绍了通过程序 VB.Net 或 C# 读取文本文件行数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1, VB.NET 读取 (通过streamReader)

  1. ' tmpCount = 0
  2.  
  3. 'Dim tmpSR As New StreamReader(fileFullName,System.Text.Encoding.Default)
  4. 'Do While tmpSR.Peek >= 0
  5. ' tmpCount = tmpCount + 1
  6. 'Loop
  7. 'tmpSR.Close()

2,通过VB.NET程序调用cmd命令

调用方法:fileRecordCounts = GetTxtRowCount(file)

fileShortName = System.IO.Path.GetFileName(file) ' 取短路径名
fileCreationDate = System.IO.File.GetCreationTime(file).ToString("yyyy MM dd HH:mm") ‘取文件创建时间
fileSize = New System.IO.FileInfo(file).Length / 1024 ’取文件大小, fileSize 为 KB

  1. Private Function GetFileRowCount_Info(ByVal sFileFullName As String) As String
  2. If (Not File.Exists(sFileFullName)) Then Return ""
  3.  
  4. Dim output As String = ""
  5. Try
  6. Dim myProcess As System.Diagnostics.Process = New System.Diagnostics.Process()
  7.  
  8. myProcess.StartInfo.FileName = "cmd.exe"
  9. myProcess.StartInfo.UseShellExecute = False
  10. myProcess.StartInfo.RedirectStandardInput = True
  11. myProcess.StartInfo.RedirectStandardOutput = True
  12.  
  13. myProcess.Start()
  14. Dim myStreamWriter As StreamWriter = myProcess.StandardInput
  15. myStreamWriter.WriteLine("find /V """" /C " + sFileFullName)
  16.  
  17. myStreamWriter.Close()
  18.  
  19. output = myProcess.StandardOutput.ReadToEnd()
  20.  
  21. myProcess.WaitForExit()
  22. Catch ex As Exception
  23. Console.WriteLine(ex)
  24. Return ""
  25. End Try
  26. Return output
  27.  
  28. End Function
  29.  
  30.  
  31. 'Dim result As Boolean = Int64.TryParse(value,number)
  32. ' If result Then
  33. ' Console.WriteLine("Converted '{0}' to {1}.",value,number)
  34. ' Else
  35. ' If value Is Nothing Then value = ""
  36. ' Console.WriteLine("Attempted conversion of '{0}' Failed.",value)
  37. ' End If
  38.  
  39. Public Function GetTxtRowCount(ByVal sFileFullName As String) As Long
  40.  
  41. If (Not File.Exists(sFileFullName)) Then Return -1
  42.  
  43. Dim sResult As String = GetFileRowCount_Info(sFileFullName)
  44. If (sResult = "") Then Return -1
  45.  
  46. Dim lResult As Long = 0
  47.  
  48. Dim lines() As String = sResult.Split(CChar(vbCrLf))
  49. Dim sTmp As String = ""
  50. For Each s As String In lines
  51. sTmp = s.TrimEnd(CChar(vbCrLf)).ToUpper()
  52. If (sTmp = "") Then Continue For
  53.  
  54. If (Not sTmp.Contains(".TXT")) Then Continue For
  55. If (Not sTmp.Contains("----------")) Then Continue For
  56.  
  57. Long.TryParse(sTmp.Split(CChar(":"))(1).Trim(),lResult) ' 这里需要根据实际情况来
  58.  
  59. Exit For
  60.  
  61. Next
  62.  
  63. Return lResult
  64. End Function
  65.  
  66. 'Public Function GetTxtRowCount(ByVal sFileFullName As String) As Integer
  67.  
  68. ' If (Not File.Exists(sFileFullName)) Then Return -1
  69.  
  70. ' Dim sResult As String = GetFileRowCount_Info(sFileFullName)
  71. ' If (sResult = "") Then Return -1
  72.  
  73. ' Dim lResult As Integer = 0
  74.  
  75. ' Dim lines() As String = sResult.Split(System.Convert.tochar("\n"))
  76. ' Dim sTmp As String = ""
  77. ' For Each s As String In lines
  78. ' sTmp = s.TrimEnd(System.Convert.tochar("\r")).ToUpper()
  79. ' If (sTmp = "") Then Continue For
  80.  
  81. ' If (Not sTmp.Contains(".TXT")) Then Continue For
  82. ' If (Not sTmp.StartsWith("----------")) Then Continue For
  83.  
  84. ' Integer.TryParse(sTmp.Split(CChar(":"))(2).Trim(),lResult)
  85.  
  86. ' Exit For
  87.  
  88. ' Next
  89.  
  90. ' Return lResult
  91. 'End Function

3, 通过C#程序调用cmd命令

调用方法: long iResult = Common.ConsoleCommand.GetTxtRowCount(sFileName);

  1. public static string GetFileRowCount_Info(string sFileFullName)
  2. {
  3. if (!File.Exists(sFileFullName)) return "";
  4.  
  5. string output = "";
  6. try
  7. {
  8. System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
  9.  
  10. myProcess.StartInfo.FileName = "cmd.exe";
  11. myProcess.StartInfo.UseShellExecute = false;
  12. myProcess.StartInfo.RedirectStandardInput = true;
  13. myProcess.StartInfo.RedirectStandardOutput = true;
  14.  
  15. myProcess.Start();
  16. StreamWriter myStreamWriter = myProcess.StandardInput;
  17.  
  18. //myStreamWriter.WriteLine(sFileFullName.Substring(0,sFileFullName.IndexOf(":") + 1));
  19. myStreamWriter.WriteLine("find /V \"\" /C " + @sFileFullName);
  20.  
  21. myStreamWriter.Close();
  22.  
  23. output = myProcess.StandardOutput.ReadToEnd();
  24.  
  25. myProcess.WaitForExit();
  26. }
  27. catch (Exception e)
  28. {
  29. Console.WriteLine(e);
  30. return "";
  31. }
  32. return output;
  33. }
  34.  
  35. public static long GetTxtRowCount(string sFileFullName)
  36. {
  37. if (!File.Exists(sFileFullName)) return -1;
  38.  
  39. string sResult = GetFileRowCount_Info(sFileFullName);
  40. if (sResult == "")
  41. return -1;
  42.  
  43. long lResult = 0;
  44.  
  45. string[] lines = sResult.Split(System.Convert.tochar("\n"));
  46. string sTmp = "";
  47. foreach (string s in lines)
  48. {
  49. sTmp = s.TrimEnd(System.Convert.tochar("\r")).ToUpper();
  50. if (sTmp == "") continue;
  51.  
  52. if (!sTmp.Contains(".TXT")) continue; // 不是.TXT的排除
  53. if (!sTmp.StartsWith("----------")) continue;
  54.  
  55. long.TryParse(sTmp.Split(':')[2].Trim(),out lResult); // 这里需要根据实际情况来
  56. break;
  57. }
  58.  
  59. return lResult;
  60. }

(结束)

猜你在找的VB相关文章