Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7397941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:48:37+00:00 2026-05-29T03:48:37+00:00

I have this code which works well: Public Function LoadBinaryFile(strFilename As String) As Byte()

  • 0

I have this code which works well:

Public Function LoadBinaryFile(strFilename As String) As Byte()
    Using fsSource As FileStream = New FileStream(strFilename, FileMode.Open, FileAccess.Read)
        ' Read the source file into a byte array.
        Dim bytes() As Byte = New Byte((fsSource.Length) - 1) {}
        Dim numBytesToRead As Integer = CType(fsSource.Length, Integer)
        Dim numBytesRead As Integer = 0

        'tsProgressBar.Minimum = 0
        'tsProgressBar.Maximum = numBytesToRead

        While (numBytesToRead > 0)
            ' Read may return anything from 0 to numBytesToRead.
            Dim n As Integer = fsSource.Read(bytes, numBytesRead, _
                numBytesToRead)
            ' Break when the end of the file is reached.
            If (n = 0) Then
                Exit While
            End If
            numBytesRead = (numBytesRead + n)
            numBytesToRead = (numBytesToRead - n)

            'tsProgressBar.Value = numBytesRead

        End While
        numBytesToRead = bytes.Length

        Return bytes

    End Using
End Function

And I have this code to save the file which also works well:

Public Function SaveBinaryFile(strFilename As String, bytesToWrite() As Byte) As Boolean
    Using fsNew As FileStream = New FileStream(strFilename, FileMode.Create, FileAccess.Write)
        fsNew.Write(bytesToWrite, 0, bytesToWrite.Length)
    End Using
End Function

What I am after is some help to modify the SaveBinaryFile function to implement a progress bar.

Final:

OK, I have written the function myself. Here it is:

    Public Function ReadBinaryFile(strFilename As String) As Byte()
    Dim position As Integer = 0
    Dim bufferSize As Integer = 4096
    Dim bytes() As Byte

    'frmMain.tsProgressBar.Value = 0

    Using fsOpen As FileStream = New FileStream(strFilename, FileMode.Open)
        redim bytes((fsOpen.Length) - 1)
        Do
            If (position + bufferSize) > fsOpen.Length Then
                fsOpen.Read(bytes, position, fsOpen.Length - position)
                Exit Do
            Else
                fsOpen.Read(bytes, position, bufferSize)
            End If
            'frmMain.tsProgressBar.Value = ((position / fsOpen.Length) * 100)
            'frmMain.tsProgressBar.Refresh()
            Application.DoEvents()
            position += bufferSize
        Loop
    End Using

    Return bytes

End Function
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T03:48:38+00:00Added an answer on May 29, 2026 at 3:48 am

    I’ve never tried using the Async options on FileStream but there don’t seem to be any event handlers.

    I would break it down into a loop, you have the total amount of bytes to write so you could loop through writing 4k at a time, update your progress bar and continue the loop.

    Public Sub SaveBinaryFile(strFilename As String, bytesToWrite() As Byte)
        Dim position As Integer = 0
    
        Using fsNew As FileStream = New FileStream(strFilename, FileMode.Create, FileAccess.Write)
            Do
                Dim intToCopy As Integer = Math.Min(4096, bytesToWrite.Length - position)
                Dim buffer(intToCopy - 1) As Byte
                Array.Copy(bytesToWrite, position, buffer, 0, intToCopy)
                fsNew.Write(buffer, 0, buffer.Length)
                ProgressBar1.Value = ((position / bytesToWrite.Length) * 100)
                Application.DoEvents()
                position += intToCopy
            Loop While position < bytesToWrite.Length
        End Using
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this existing code which works very well. public static T SessionGet<T>(string key)
I have this code which compiles and works as expected: class Right {}; class
I have this code, which works fine, but I would like to be able
I have this code in my cfm, which works <cfif not StructIsEmpty(form)> <cfset larray
I have this code which is called at an onChange event of an function
I have this code (which is way simplified from the real code): public interface
I have a js code in which an array works well when its like
I have this code which gets WP posts, but it gets all the posts
I have this code which will include template.php file from inside each of these
I have this code which should create a splash image with either no animation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.