File copy with progress bar

Posted on 2009-04-21
Last Modified: 2013-11-07
I have a solution that works.  I Found a function that copies the file using FileStream.  The issue I have is that when my program runs and gets to where the progress bar starts to update, the memory usage for my program jumps to almost 400MB.  

The purpose of my program is to copy an oracle export file.  These files can range from 20MB to several GB.  I want my user to know how far along the copy is.

 My question is am I doing this correctly and if not what is the correct way to accomplish.  

Private Sub CopyFile(ByVal OldFile As String, ByVal NewFile As String)

        Dim n As Integer

        Me.pbCopyProgress.Value = 0

        Dim FS As New FileStream(OldFile, FileMode.Open)

        Dim FW As New FileStream(NewFile, FileMode.Truncate, FileAccess.ReadWrite)

        Dim Buffer() As Byte

        'Get the bytes from file to a byte array

        ReDim Buffer(FS.Length - 1)

        Me.pbCopyProgress.Maximum = (FS.Length / 100) + 1000

        FS.Read(Buffer, 0, Buffer.Length)

        'Do your stuff :-)

        For i As Int32 = 0 To Buffer.Length - 1

            n += 1

            If n = 100 Then

                Me.pbCopyProgress.Value += 1

                n = 0

            End If





    End Sub

Open in new window

Question by:revo1059
    LVL 48

    Accepted Solution

    It looks right, but you have to include an Application.DoEvents after the Me.pbCopyProgress.Value += 1 to let the system work.

    LVL 19

    Assisted Solution

    LVL 1

    Author Comment

    I found different way to do it

        Private Sub CopyFile(ByVal OldFile As String, ByVal NewFile As String)
            Dim m_aBuffer(1024) As Byte
            Dim m_iRemotePort, m_iBytes As Int64

            pbCopyProgress.Value = 0
            UpdateLogWindow("File size= " & (GetFileSize(OldFile) / 1024))
            pbCopyProgress.Maximum = (GetFileSize(OldFile) / 1024)

            Dim FS As New FileStream(OldFile, FileMode.Open, FileAccess.Read)
            Dim FW As New FileStream(NewFile, FileMode.Truncate, FileAccess.ReadWrite)
            Do While (True)
                m_aBuffer.Clear(m_aBuffer, 0, m_aBuffer.Length)

                m_iBytes = FS.Read(m_aBuffer, 0, m_aBuffer.Length)
                FW.Write(m_aBuffer, 0, m_iBytes)
                pbCopyProgress.Value += (m_iBytes / 1024)
                If (m_iBytes <= 0) Then
                    Exit Do
                End If

        End Sub

    I already had a function to get the file size for something else I needed, so I set the pb max to file size.  Using this method, the memory usage is perfectly normal.  

    Thanks for your replies, I will split the points

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    In my previous article ( we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    759 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

    14 Experts available now in Live!

    Get 1:1 Help Now