Link to home
Start Free TrialLog in
Avatar of revo1059
revo1059Flag for United States of America

asked on

File copy with progress bar

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
            FW.WriteByte(Buffer(i))
        Next
        FS.Close()
        FW.Close()
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of revo1059

ASKER

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
        Loop

        FS.Close()
        FW.Close()
    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