[英]Reading line count using vb.net
如何计算编号 我们上传的文件中的行数(可能是文本文件,csv或excel文件)。 我使用代码来获取记录计数,如下所示:
Dim FileCount = From lin1 As String In File.ReadAllLines(hidFilePath.Value.ToString())
Let dd = lin1.Count
Select dd
Dim file_count As Integer = FileCount.Count
但在某些情况下,即时通讯的计数错误 它没有显示确切的计数。
请帮助,我应该能够在不循环文件中每条记录的情况下获得计数。
谢谢。
ReadAllLines
返回一个字符串数组,因此您可以简单地获取数组的长度。
Dim file_count As Integer = _
File.ReadAllLines(hidFilePath.Value.ToString()).Length
编辑:快速阅读问题并用循环解决方案回答
您可以将行数设置为整数,并使阅读器读取到文件末尾。
Dim sr As New StreamReader("file path here")
Dim lineCount As Integer = System.Text.RegularExpressions.Regex.Split(sr.ReadToEnd(), Environment.NewLine).Length
sr.Close()
您可以使用count变量并遍历文件,直到没有任何内容为止
''name count and set it to 0
Dim count As Integer
count = 0
Dim obj As StreamReader
obj = New StreamReader("C:\...\source.txt")
''loop through the file until the end
Do Until obj.ReadLine Is Nothing
count = count + 1
Loop
''close file and show count
obj.Close()
MessageBox.Show(count)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.