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

Posted on 2009-12-26
Last Modified: 2012-05-08
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.

Question by:nvbob
    LVL 85

    Accepted Solution

    *** 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

    LVL 10

    Expert Comment

    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

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now