How can I read/stream a file without loading the entire file into memory?
Here's an example of how to read a file in chunks of 1KB without loading the entire contents into memory:
const int chunkSize = 1024; // read the file by chunks of 1KB
using (var file = File.OpenRead("foo.dat"))
{
int bytesRead;
var buffer = new byte[chunkSize];
while ((bytesRead = file.Read(buffer, 0, buffer.Length)) > 0)
{
// TODO: Process bytesRead number of bytes from the buffer
// not the entire buffer as the size of the buffer is 1KB
// whereas the actual number of bytes that are read are
// stored in the bytesRead integer.
}
}
System.IO.FileStream
does not load the file into memory.
This stream is seekable and MD5 hashing algorithm doesn't have to load the stream(file) intro memory either.
Please replace file_path
with the path to your file.
byte[] hash = null;
using (var stream = new FileStream(file_path, FileMode.Open))
{
using (var md5 = new System.Security.Cryptography.MD5CryptoServiceProvider())
{
hash = md5.ComputeHash(stream);
}
}
Here, your MD5 Hash will be stored in the hash
variable.