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

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

                ' tmpCount = 0

                'Dim tmpSR As New StreamReader(fileFullName, System.Text.Encoding.Default)
                'Do While tmpSR.Peek >= 0
                '    tmpCount = tmpCount + 1
                'Loop
                '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

 Private Function GetFileRowCount_Info(ByVal sFileFullName As String) As String
        If (Not File.Exists(sFileFullName)) Then Return ""

        Dim output As String = ""
        Try
            Dim myProcess As System.Diagnostics.Process = New System.Diagnostics.Process()

            myProcess.StartInfo.FileName = "cmd.exe"
            myProcess.StartInfo.UseShellExecute = False
            myProcess.StartInfo.RedirectStandardInput = True
            myProcess.StartInfo.RedirectStandardOutput = True

            myProcess.Start()
            Dim myStreamWriter As StreamWriter = myProcess.StandardInput
            myStreamWriter.WriteLine("find /V """" /C " + sFileFullName)

            myStreamWriter.Close()

            output = myProcess.StandardOutput.ReadToEnd()

            myProcess.WaitForExit()
        Catch ex As Exception
            Console.WriteLine(ex)
            Return ""
        End Try
  
        Return output

    End Function


    'Dim result As Boolean = Int64.TryParse(value, number)
    '  If result Then
    '     Console.WriteLine("Converted '{0}' to {1}.", value, number)
    '  Else
    '     If value Is Nothing Then value = "" 
    '     Console.WriteLine("Attempted conversion of '{0}' failed.", value)
    '  End If    

    Public Function GetTxtRowCount(ByVal sFileFullName As String) As Long

        If (Not File.Exists(sFileFullName)) Then Return -1

        Dim sResult As String = GetFileRowCount_Info(sFileFullName)
        If (sResult = "") Then Return -1

        Dim lResult As Long = 0

        Dim lines() As String = sResult.Split(CChar(vbCrLf))
        Dim sTmp As String = ""
        For Each s As String In lines
            sTmp = s.TrimEnd(CChar(vbCrLf)).ToUpper()
            If (sTmp = "") Then Continue For

            If (Not sTmp.Contains(".TXT")) Then Continue For
            If (Not sTmp.Contains("----------")) Then Continue For

            Long.TryParse(sTmp.Split(CChar(":"))(1).Trim(), lResult)  ' 这里需要根据实际情况来

            Exit For

        Next

        Return lResult
    End Function

    'Public Function GetTxtRowCount(ByVal sFileFullName As String) As Integer

    '    If (Not File.Exists(sFileFullName)) Then Return -1

    '    Dim sResult As String = GetFileRowCount_Info(sFileFullName)
    '    If (sResult = "") Then Return -1

    '    Dim lResult As Integer = 0

    '    Dim lines() As String = sResult.Split(System.Convert.ToChar("\n"))
    '    Dim sTmp As String = ""
    '    For Each s As String In lines
    '        sTmp = s.TrimEnd(System.Convert.ToChar("\r")).ToUpper()
    '        If (sTmp = "") Then Continue For

    '        If (Not sTmp.Contains(".TXT")) Then Continue For
    '        If (Not sTmp.StartsWith("----------")) Then Continue For

    '        Integer.TryParse(sTmp.Split(CChar(":"))(2).Trim(), lResult)

    '        Exit For

    '    Next

    '    Return lResult
    'End Function

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

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

        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;
        }

(结束)

你可能感兴趣的:(通过程序 VB.Net 或 C# 读取文本文件行数)