@bobnoordam

Static class implementing a filecopy function while showing a progress bar

Imports System.IO
  
''' <summary>
''' Copy a file from the source to the target location, and shows a progressbar to the user
'''
''' Call:
'''
'''     CopyFileWithBar.DoCopy("c:\somesource.bin","c:\sometarget.bin")
'''
''' Returns FALSE if the source file does not exists
''' Returns TRUE if the copy succeeded
''' You need to trap exceptions for e.g. non writable target files in the wrapping code using this class.
'''
''' H.P. Noordam (b.noordam@nhcoding.nl)
'''
''' </summary>
''' <remarks></remarks>
Public Class CopyFileWithBar
  
    Public Shared Function DoCopy(ByVal sSourcefile As String, _
                             ByVal sTargetfile As String) As Boolean
  
        If File.Exists(sSourcefile) = False Then Return False
  
        ' --- Create a form to show the progress of the copy action
        Dim fStatus As New Form
        fStatus.Width = 640
        fStatus.Height = 60
        fStatus.StartPosition = FormStartPosition.CenterScreen
        fStatus.FormBorderStyle = FormBorderStyle.FixedSingle
        fStatus.TopMost = True
        ' //
  
        ' --- Create a new progressbar, and place the control on the form
        Dim oProgressbar As New ProgressBar
        oProgressbar.Width = 600
        oProgressbar.Height = 30
        oProgressbar.Minimum = 0
        oProgressbar.Maximum = 100
        oProgressbar.Location = New Point(10, 5)
        ' //
  
        ' --- Place the progressbar on the form, and display the progress box
        '     (on windows 7 the progressbar tends to lack behind on smaller files
        '      this is a side effect caused by the control, not by the code. If you
        '      realy want to address the issue you need to seriously slow down the
        '      copy by implemting something like System.Threading.Thread.Sleep(50)
        '      in the main loop)
        fStatus.Controls.Add(oProgressbar)
        fStatus.Show()
        ' //
  
        ' Copy the file
        Using oInfile As New FileStream(sSourcefile, FileMode.Open, FileAccess.Read)
            Using oOutfile As New FileStream(sTargetfile, FileMode.Create, FileAccess.Write)
  
                Dim oReader As New BinaryReader(oInfile)
                Dim oWriter As New BinaryWriter(oOutfile)
  
                Dim nFilesize As Int64 = oReader.BaseStream.Length
                Dim nProgress As Integer
  
                Dim bByte(32768) As Byte
                Dim nCount As Int64
  
                Dim vExit As Boolean = False
                                While vExit = False
  
                    bByte = oReader.ReadBytes(32768)
                    oWriter.Write(bByte)
  
                    Dim nReadbytes As Integer = bByte.Count
                    nCount = nCount + nReadbytes
  
                    ' --- Update display
                    If nCount <> 0 Then
                        nProgress = CInt((nCount / (nFilesize) * 100))
                        oProgressbar.Value = nProgress
                        fStatus.Text = String.Format("{0}%", nProgress)
                        Application.DoEvents()
                    End If
                    ' //
  
                    If nReadbytes = 0 Then
                        vExit = True
                    End If
  
                End While
  
                oReader.Close()
                oWriter.Close()
  
            End Using
        End Using
        ' //
  
        fStatus.Close()
  
        Return True
  
    End Function
  
End Cla