Advertisement
Bucher100

File copy with progressbar

Oct 28th, 2024
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 0.93 KB | Source Code | 0 0
  1. 'Create the file stream for the source file
  2. Dim streamRead as New System.IO.FileStream([sourceFile], System.IO.FileMode.Open)
  3. 'Create the file stream for the destination file
  4. Dim streamWrite as New System.IO.FileStream([targetFile], System.IO.FileMode.Create)
  5. 'Determine the size in bytes of the source file (-1 as our position starts at 0)
  6. Dim lngLen as Long = streamRead.Length - 1
  7. Dim byteBuffer(1048576) as Byte   'our stream buffer
  8. Dim intBytesRead as Integer    'number of bytes read
  9.  
  10. While streamRead.Position < lngLen    'keep streaming until EOF
  11.     'Read from the Source
  12.     intBytesRead = (streamRead.Read(byteBuffer, 0, 1048576))
  13.     'Write to the Target
  14.     streamWrite.Write(byteBuffer, 0, intBytesRead)
  15.     'Display the progress
  16.     ProgressBar1.Value = CInt(streamRead.Position / lngLen * 100)
  17.     Application.DoEvents()    'do it
  18. End While
  19.  
  20. 'Clean up
  21. streamWrite.Flush()
  22. streamWrite.Close()
  23. streamRead.Close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement