Areeg Ghonaim
Areeg Ghonaim

Reputation: 13

vb.net How to read a small part of a very large file not using memory?

I use a console app to work on large files. I use

IO.File.ReadAllBytes(OpenFileDlg.FileName)

but it loads all the large file on memory and stops when the memory is full , without reading it completely. I want to split the large file or read a part of it, "without loading all the file on memory, so it would work on small RAM"

Upvotes: 0

Views: 692

Answers (1)

kshkarin
kshkarin

Reputation: 584

Use FileStream to read in chunks of 4096 bytes then encrypt them (or do any necessary processing) and then write them to the new file stream, since it's streaming in/out it won't hog up all the memory trying to read/write everything at once, simple example:

Dim bytesRead As Integer
Dim buffer(4096) As Byte 'Default buffer size is 4096
Using inFile As New IO.FileStream("C:\\Temp\\inFile.bin", FileMode.Open)
    Using outFile As New IO.FileStream("C:\\Temp\\outFile.bin", FileMode.Append, FileAccess.Write)
        Do
            bytesRead = inFile.Read(buffer, 0, buffer.Length)
            If bytesRead > 0 Then
                ' Do encryption or any necessary processing of the bytes
                outFile.Write(buffer, 0, bytesRead)
            End If
        Loop While bytesRead > 0
    End Using
End Using

Upvotes: 1

Related Questions