• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 411
  • Last Modified:

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.

1 Solution
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


Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now