Open an Text File and Count the No of Line in VB.Net

Abstract:


Create an text file and count the no of line to read from text file.

Description:


To create an text file and count the no of line to read from text file in VB.net.

Code:




Imports System
Imports System.Drawing
Imports System.Data
Imports System.IO
Imports System.Collections
Imports System.Windows.Forms
Imports System.Drawing.Printing

Public Class TextFileCreation
Shared Sub Main()
Dim myReader As StreamReader

Try
myReader = File.OpenText("TextFile.txt")
Catch e As IOException
Console.WriteLine(e.ToString)
Console.WriteLine(e.Message)
Exit Sub
End Try

Dim lineCounter As Integer = 0
Dim currentLine As String, currentData As String

Do
Try
currentLine = myReader.ReadLine
lineCounter = lineCounter + 1
Catch e As EndOfStreamException
Console.WriteLine(e.Message)
Finally
currentData = currentData & currentLine & vbCrLf
End Try
Loop While currentLine <> Nothing

Console.WriteLine("Number of lines : " & lineCounter - 1 )

myReader.Close()
myReader = Nothing
End Sub
End Class


Comments

Author: Mrs. Meetu Choudhary Nanda03 Jul 2009 Member Level: Gold   Points : 1

Format your resources Properly to get good points and more readers.


++

Thanks and Regards
Meetu Choudhary
Site Coordinator.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: