how to determine how many bytes have been read using VB.Net 2008

I am using VB.Net 2008.  
Reading a file using the following

        Using MyReader As New FileIO.TextFieldParser(Me.txtGetFile.Text)

            MyReader.TextFieldType = FileIO.FieldType.Delimited
            Dim aCurrentRow As String()

             aCurrentRow = MyReader.ReadFields()
How can I determine how many bytes have been read?

I want to use a prgress bar comparing the number of bytes read to the total file size.  The source file contains ~7MM rows and is ~400MB.

Thanks in advance.

Who is Participating?
Mike TomlinsonMiddle School Assistant TeacherCommented:
*** Untested *** try passing a FileStream to TextFieldParser instead so you can use the underlying Position() and Length() properties.  I think that .Net "reads ahead", though, so it may not be 100% accurate:
        Using fs As New System.IO.FileStream(Me.txtGetFile.Text, IO.FileMode.Open)
            Using MyReader As New FileIO.TextFieldParser(fs)
                MyReader.TextFieldType = FileIO.FieldType.Delimited
                Dim percent As Double
                Dim aCurrentRow As String()
                While Not MyReader.EndOfData
                    aCurrentRow = MyReader.ReadFields()
                    percent = fs.Position / fs.Length * 100
                    Debug.Print("Percent: " & percent & "%")
                End While
            End Using
        End Using

Open in new window

TextFieldParser.ReadFields Method reads all fields on the current line, returns them as an array of strings.
Therefore you can get the size of array of strings using the following way.

Dim totalBytes As Integer = 0
'Repeat the following lines to read all the lines
Dim aCurrentRow As String()
aCurrentRow = MyReader.ReadFields()
Dim ascii As ASCIIEncoding = New ASCIIEncoding()
Dim rowBytes As Integer = 0
For Each s As String In aCurrentRow
      rowBytes = N + ascii.GetByteCount(s)
totalBytes = totalBytes + N

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.