public static string GetMD5Code(string filePath) { FileStream stream = null; try { stream = File.OpenRead(filePath); } catch { try { stream.Close(); stream.Dispose(); } catch { } return(Consts.EmptyFileMD5); } if (stream.Length == 0) { stream.Close(); stream.Dispose(); return(Consts.EmptyFileMD5); } byte[] data = new byte[10240]; long size = stream.Length; int sizetoread; MD5Hash md5 = new MD5Hash(); md5.InitNewHash(); while (size != 0) { sizetoread = size > 10240 ? 10240 : (int)size; stream.Read(data, 0, sizetoread); //data = reader.ReadBytes((int)sizetoread); size -= sizetoread; if (size == 0) { md5.UpdateHash(data, sizetoread, true); } else { md5.UpdateHash(data, sizetoread, false); } } stream.Close(); stream.Dispose(); md5.FinalizeHash(); return(MD5Hash.FormatHash(md5.GetFinalHash(), 0, 0, 0, 1));//0��ָ0�зָ0�ظ����ٴΣ�1��д���(0Сд)myxbing }
public static string GetMD5Code(Stream stream) { if (stream == null || stream.Length == 0) { return(Consts.EmptyFileMD5); } //return Guid.NewGuid().ToString(); //next should be rebuilder. //BinaryReader reader = new BinaryReader(stream); long oldPosition = stream.Position; stream.Position = 0; byte[] data = new byte[10240]; long size = stream.Length; int sizetoread; MD5Hash md5 = new MD5Hash(); md5.InitNewHash(); while (size != 0) { sizetoread = size > 10240 ? 10240 : (int)size; stream.Read(data, 0, sizetoread); //data = reader.ReadBytes((int)sizetoread); size -= sizetoread; if (size == 0) { md5.UpdateHash(data, sizetoread, true); } else { md5.UpdateHash(data, sizetoread, false); } } md5.FinalizeHash(); stream.Position = oldPosition; //reader.Close(); //stream.Close();���ܹرգ��������� return(MD5Hash.FormatHash(md5.GetFinalHash(), 0, 0, 0, 1));//0��ָ0�зָ0�ظ����ٴΣ�1��д���(0Сд)myxbing }