可能的重复:
在 C# 中对大文件(超过 2GB)进行哈希 SHA1 https://stackoverflow.com/questions/6094306/hash-sha1-large-files-over-2gb-in-c-sharp
我有一个很大的文件,它给了我错误“抛出了‘System.OutOfMemoryException’类型的异常。”
任何人都有解决此问题的想法或解决方案。请帮忙。
示例代码....
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;
}
我在上面的代码下面的行遇到异常......
byte[] filebytes = System.IO.File.ReadAllBytes(filePath);
filePath 包含大小 > 500MB 的文件。
无需将整个文件读入内存,只需将流传递给 ComputeHash
using(var file = File.Open(path,FileMode.Open))
{
hashValue = hashString.ComputeHash(file);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)