有没有更好的方法来计算文本文件中的行?

Muh*_*ana 8 vb.net visual-studio-2010 streamreader text-files

以下是我一直在使用的内容.虽然它确实有效,但我的程序在尝试计算一个相当大的文件时会锁定,例如10,000行或更多行.较小的文件立即运行.

是否有更好的或者我应该说更快的方式来计算文本文件中的行?

这是我目前正在使用的:

    Dim selectedItems = (From i In ListBox1.SelectedItems).ToArray()
    For Each selectedItem In selectedItems
        ListBox2.Items.Add(selectedItem)
        ListBox1.Items.Remove(selectedItem)

        Dim FileQty = selectedItem.ToString
        'reads the data file and returns the qty
        Dim intLines As Integer = 0
        'Dim sr As New IO.StreamReader(OpenFileDialog1.FileName)
        Dim sr As New IO.StreamReader(TextBox1_Path.Text + "\" + FileQty)
        Do While sr.Peek() >= 0
            TextBox1.Text += sr.ReadLine() & ControlChars.CrLf
            intLines += 1
        Loop
        ListBox6.Items.Add(intLines)
    Next
Run Code Online (Sandbox Code Playgroud)

gli*_*ite 31

Imports System.IO.File 'At the beginning of the file

Dim lineCount = File.ReadAllLines("file.txt").Length
Run Code Online (Sandbox Code Playgroud)

看到这个问题.

  • 大声笑,调整.答案已经在VB中,但他只是偶然添加了分号.对不起,我已经立法了,不得不指出这一点. (4认同)