FileStream.Read Method Reads a block of bytes
Imports System
Imports System.IO
Class Test
Public Shared Sub Main()
Dim pathSource As String = "c:\tests\source.txt"
Dim pathNew As String = "c:\tests\newfile.txt"
Using fsSource As FileStream = New FileStream(pathSource,FileMode.Open, FileAccess.Read)
Dim bytes() As Byte = New Byte((fsSource.Length) - 1) {}
Dim numBytesToRead As Integer = CType(fsSource.Length,Integer)
Dim numBytesRead As Integer = 0
While (numBytesToRead > 0)
Dim n As Integer = fsSource.Read(bytes, numBytesRead,numBytesToRead)
If (n = 0) Then
Exit While
End If
numBytesRead = (numBytesRead + n)
numBytesToRead = (numBytesToRead - n)
End While
numBytesToRead = bytes.Length
Using fsNew As FileStream = New FileStream(pathNew,FileMode.Create, FileAccess.Write)
fsNew.Write(bytes, 0, numBytesToRead)
End Using
End Using
End Sub
End Class
Related examples in the same category