Public Function CountFileLines(ByVal FileName As String) As Integer
Dim buffer(32 * 1024) As Char
Dim reader As System.IO.TextReader
Dim i As Integer
Dim read As Integer
Dim total As Integer = 0
'si le fichier existe
If System.IO.File.Exists(FileName) Then
'on ouvre le fichier
reader = System.IO.File.OpenText(FileName)
read = reader.Read(buffer, 0, buffer.Length)
While (read > 0)
i = 0
While i < read
If buffer(i) = Chr(10) Then total += 1
i += 1
End While
read = reader.Read(buffer, 0, buffer.Length)
End While
If Not buffer(i - 1) = Chr(10) Then total += 1 'facultatif, pour les fichiers avec un dernier retour chariot
'nettoyage
Erase buffer
reader.Close()
reader.Dispose()
reader = Nothing
End If
Return total
End Function
' la fonction CHR venant de VB6, on évite la référence Microsoft.VisualBasic
Private Function Chr(ByVal CharCode As Integer) As Char
Return Char.ConvertFromUtf32(CharCode)
End Function