Hack Forums
[.NET] [VB.NET]Packetwise File Copy - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] [VB.NET]Packetwise File Copy (/showthread.php?tid=400969)



[VB.NET]Packetwise File Copy - codepoet - May 3, 2010

Description:

This function is to copy a file via many small packets, like the real copy functions do.

Code
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

originally posted on dotnet-snippets.com

[img]http://i685.photobucket.com/albums/vv215/blackpearl4/THANKS.gif[/img]
If You Like this tutorial, please check out my other tutorials by clicking ==> H3R3