How to calculate the number of rows in a file which we have uploaded. The file might be either a text file or a CSV or Exce开发者_开发技巧l file.
I'm using the folowing code to get the record 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 the count is wrong.
ReadAllLines
returns a string array, so you can simply take the length of the array.
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
''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)
精彩评论