[英]Reading line count using vb.net
How to calculate the no. 如何计算编号 of rows in the file which we have uploaded that might be either a text file or csv or excel file.
我们上传的文件中的行数(可能是文本文件,csv或excel文件)。 Im using the code to get the records count like this:
我使用代码来获取记录计数,如下所示:
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
but in some cases im getting wrong count. 但在某些情况下,即时通讯的计数错误 it is not showing the exact count.
它没有显示确切的计数。
Please help and i should be able to get the count without looping each record in the file. 请帮助,我应该能够在不循环文件中每条记录的情况下获得计数。
Thanks. 谢谢。
ReadAllLines
returns a string array, so you can simply take the length of the array. ReadAllLines
返回一个字符串数组,因此您可以简单地获取数组的长度。
Dim file_count As Integer = _
File.ReadAllLines(hidFilePath.Value.ToString()).Length
EDIT: read question to fast and answered with a loop solution 编辑:快速阅读问题并用循环解决方案回答
You can set your linecount as an integer and have the reader read to the end of the file. 您可以将行数设置为整数,并使阅读器读取到文件末尾。
Dim sr As New StreamReader("file path here")
Dim lineCount As Integer = System.Text.RegularExpressions.Regex.Split(sr.ReadToEnd(), Environment.NewLine).Length
sr.Close()
You can use a count variable and loop through the file until theres nothing 您可以使用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.