Results 1 to 3 of 3

Thread: [Resolved] ReadAllBytes fails reading 1GB file

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Resolved [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.

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    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

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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();
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width