阅读使用字节大文件

阅读使用字节大文件

本文介绍了阅读使用字节大文件[]给出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  在C#中 SHA1散列大文件(超过2GB)

我而言规模较大的文件,这让我错误类型'System.OutOfMemoryException的'被抛出的异常。

I have a large file in size of terms and it gives me error "Exception of type 'System.OutOfMemoryException' was thrown."

任何人是有思想或解决方案解决此问题。请帮忙。
示例code ....

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;
    }

我正在逐渐线异常低于上述code ....

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

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

文件路径是有它有大小> 500MB的文件。

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

推荐答案

而不是读取整个文件到内存只是传递一个流ComputeHash

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);
}

这篇关于阅读使用字节大文件[]给出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!