Reading large file using byte[] gives error

Possible Duplicate:
Hash SHA1 large files (over 2gb) in C#

I have a large file in size of terms and it gives me error “Exception of type ‘System.OutOfMemoryException’ was thrown.”

Anyone is having idea or solution for resolve this issue. Please help.
Sample code….

 private string GetSha1()
    {
        string filePath = txtFileName.Text;
        byte[] filebytes = System.IO.File.ReadAllBytes(filePath);
        byte[] hashValue;
        SHA1Managed hashString = new SHA1Managed();

        string hex = "";

        hashValue = hashString.ComputeHash(filebytes);
        foreach (byte x in hashValue)
        {
            hex += String.Format("{0:x2}", x);
        }
        return hex;
    }

I am getting exception at line below in above code….

   byte[] filebytes = System.IO.File.ReadAllBytes(filePath);

The filePath is having file which is having size > 500MB.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Instead of reading the whole file into memory just pass a stream to ComputeHash

using(var file = File.Open(path,FileMode.Open))
{
   hashValue = hashString.ComputeHash(file);
}

Method 2

Well you’ve pretty much explained what the problem is. You’re trying to read a 500MB file straight into a byte[] because you’re using ReadAllBytes(). This isn’t really suitable for anything except small files.

If you’re wanting to compute a hash for a file, just use a stream as the argument:

using (filestream f = File.Open(path,FileMode.Open))
{
    hashValue = hashString.ComputeHash(f);
}

Method 3

Maybe you should use System.IO.File.Read and just read a chunk of the file into your byte array at a time instead of reading the entire file at once.

See this example on MSDN on using Read.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x