VB - Packetwise File Copy

By , 4/10/2008
(1 ratings)
This function is to copy a file via many small packets, like the real copy functions do.
Translate to C#
Imports System.IO

Public Function CopyFile(ByVal Source As String, ByVal Target As String, Optional ByVal BufferSize As Long = 51200) As Boolean
    Try
        Dim SourceFileStream As New FileStream(Source, FileMode.Open)
        Dim TargetFileStream As New FileStream(Target, FileMode.Create)

        Dim Data(BufferSize) As Byte
        Dim Buffer As Long = 0
        Dim BufferCount As Long = 0
        Dim FileSize As Long = SourceFileStream.Length

        Do
            If BufferCount >= FileSize - BufferSize Then
                ReDim Data(FileSize - BufferCount)
                Buffer = SourceFileStream.Read(Data, 0, FileSize - BufferCount)
                TargetFileStream.Write(Data, 0, Buffer)
                Exit Do
            End If

            Buffer = SourceFileStream.Read(Data, 0, BufferSize)
            TargetFileStream.Write(Data, 0, BufferSize)
            BufferCount += Buffer
        Loop
        SourceFileStream.Close()
        TargetFileStream.Close()
    Catch
        Return False
    End Try
End Function

Tagged with File, Copy.

Comments

 

Log in, to comment!