Here is my method:
CODE
Private Sub CopyFile(ByVal OldFile As String, ByVal NewFile As String)
Const bufferLength As Integer = 1000 'Number of bytes to do at once
log(OldFile + " -> " + NewFile) 'Log the fact that we are copying these files
Try
'Create the directory if it does not exist
Directory.CreateDirectory(NewFile.Substring(0, NewFile.LastIndexOf("\")))
'Open the files
Dim FS As New FileStream(OldFile, FileMode.Open)
Dim FW As New FileStream(NewFile, FileMode.Create)
'Initialize a buffer to hold the data in ram temporarily
Dim Buffer(bufferLength) As Byte
'Initialize "Pointers" of sorts to deal with the buffer
'Copy the file
Dim cur As Long
For cur = 0 To (FS.Length - 1) Step bufferLength
'if whats left in the file cannot fill our buffer, than we want it so the buffer does not have garbage for the
'of the file. So, if the amount of distance left is less than the bufferlength, jump to the else
If (FS.Length - FS.Position) > bufferLength Then
FS.Read(Buffer, 0, bufferLength)
FW.Write(Buffer, 0, bufferLength)
RaiseEvent fileProgressUpdate(CInt(cur / FS.Position * 100))
Else
Dim tmp As Integer = CInt((FS.Length - FS.Position))
RaiseEvent fileProgressUpdate(100)
FS.Read(Buffer, 0, bufferLength)
FW.Write(Buffer, 0, tmp)
End If
Next
FS.Close()
FW.Close()
RaiseEvent fileProgressUpdate(0)
Catch e As Exception
RaiseEvent fileProgressUpdate(0)
log("ERROR: " + e.Message)
End Try
End Sub
Const bufferLength As Integer = 1000 'Number of bytes to do at once
log(OldFile + " -> " + NewFile) 'Log the fact that we are copying these files
Try
'Create the directory if it does not exist
Directory.CreateDirectory(NewFile.Substring(0, NewFile.LastIndexOf("\")))
'Open the files
Dim FS As New FileStream(OldFile, FileMode.Open)
Dim FW As New FileStream(NewFile, FileMode.Create)
'Initialize a buffer to hold the data in ram temporarily
Dim Buffer(bufferLength) As Byte
'Initialize "Pointers" of sorts to deal with the buffer
'Copy the file
Dim cur As Long
For cur = 0 To (FS.Length - 1) Step bufferLength
'if whats left in the file cannot fill our buffer, than we want it so the buffer does not have garbage for the
'of the file. So, if the amount of distance left is less than the bufferlength, jump to the else
If (FS.Length - FS.Position) > bufferLength Then
FS.Read(Buffer, 0, bufferLength)
FW.Write(Buffer, 0, bufferLength)
RaiseEvent fileProgressUpdate(CInt(cur / FS.Position * 100))
Else
Dim tmp As Integer = CInt((FS.Length - FS.Position))
RaiseEvent fileProgressUpdate(100)
FS.Read(Buffer, 0, bufferLength)
FW.Write(Buffer, 0, tmp)
End If
Next
FS.Close()
FW.Close()
RaiseEvent fileProgressUpdate(0)
Catch e As Exception
RaiseEvent fileProgressUpdate(0)
log("ERROR: " + e.Message)
End Try
End Sub
The only reason I am going through something so complex for something so simple is to get a progressbar on the screen (which is what the FileProgressUpdate event is for).
My problem is that the else block in the for loop:
CODE
Dim tmp As Integer = CInt((FS.Length - FS.Position))
RaiseEvent fileProgressUpdate(100)
FS.Read(Buffer, 0, bufferLength)
FW.Write(Buffer, 0, tmp)
RaiseEvent fileProgressUpdate(100)
FS.Read(Buffer, 0, bufferLength)
FW.Write(Buffer, 0, tmp)
does not seem to be executing. Every file I run this thing on has the last bufferLength bytes cut off the end.
One more thing, I tried just removing that if...else block alltogether and having it read/write bufferlength every time, but that writes a ton of garbage at the end of the file.
Because this is going into a backup program, its kinda important that it can copy a file.
Any ideas?
