|
-
Aug 7th, 2006, 08:57 AM
#1
Thread Starter
Frenzied Member
[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);
}
Last edited by wey97; Oct 20th, 2006 at 07:33 AM.
-
Aug 7th, 2006, 11:41 AM
#2
Re: ReadAllBytes fails reading 1GB file
What do you want to do with the file?
your code and your question explains nothings
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Aug 7th, 2006, 06:03 PM
#3
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();
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|