I need to calculate MD5 for a file.
private string GetMD5(string file)
{
using var md5 = MD5.Create();
using var stream = new StreamReader(file);
return (BitConverter.ToString(md5.ComputeHash(stream.BaseStream)).Replace("-", string.Empty)).ToLower();
}
private string GetMD5_V2(string file)
{
using var md5 = MD5.Create();
using var stream = new StreamReader(file);
**_ = stream.EndOfStream;**
return (BitConverter.ToString(md5.ComputeHash(stream.BaseStream)).Replace("-", string.Empty)).ToLower();
}
test()
{
var fichier = "myFile.txt";
var md5_1 = GetMD5(fichier);
var md5_2 = GetMD5_V2(fichier);
}
When I run this code md5_1 and md5_2 is different. I not understand why when I read the propertie stream.EndOfStream this change the result of stream.BaseStream?