[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1707
  • Last Modified:

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

0
revo1059
Asked:
revo1059
2 Solutions
 
jpaulinoCommented:
Hi,
It looks right, but you have to include an Application.DoEvents after the Me.pbCopyProgress.Value += 1 to let the system work.

0
 
revo1059Author Commented:
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
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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