[Resolved] ReadAllBytes fails reading 1GB file
When I try to create a byte array to read a file that's 1GB in size, I get System.OutOfMemoryException. I assume this means there isn't enough system memory to allocate an array of bytes this large.
Is there some other way I can read this file, possibly in chunks, to deal with it?
Code:
System.IO.FileInfo fi = new System.IO.FileInfo(txtRead.Text);
byte[] bt;
try
{
bt = new byte[fi.Length];
bt = System.IO.File.ReadAllBytes(txtRead.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Re: ReadAllBytes fails reading 1GB file
What do you want to do with the file?
your code and your question explains nothings
Re: ReadAllBytes fails reading 1GB file
Create a FileStream object and call it's Read method:
Code:
const int BUF_SIZE = 1024;
System.IO.FileStream fs = new System.IO.FileStream("file path here", System.IO.FileMode.Open);
byte[] buf = new byte[BUF_SIZE];
int bytesRead;
// Read the file one kilobyte at a time.
do
{
bytesRead = fs.Read(buf, 0, BUF_SIZE);
// 'buf' contains the last 1024 bytes read of the file.
} while (bytesRead == BUF_SIZE);
fs.Close();