问题
We have an embedded resource and need to get the md5 hash of the file before extracting it in order to know if it is different from an already existing file, (becouse if we have to extract it to compare them it would be better to replace the file directly)
Any suggestion is appreciated
回答1:
What sort of embedded resource is it? If it's one you get hold of using Assembly.GetManifestResourceStream()
, then the simplest approach is:
using (Stream stream = Assembly.GetManifestResourceStream(...))
{
using (MD5 md5 = MD5.Create())
{
byte[] hash = md5.ComputeHash(stream);
}
}
If that doesn't help, please give more information as to how you normall access/extract your resource.
回答2:
You can use MemoryStream
using (MemoryStream ms = new MemoryStream(Properties.Resources.MyZipFile))
{
using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
{
byte[] hash = md5.ComputeHash(ms);
string str = Convert.ToBase64String(hash);
// result for example: WgWKWcyl2YwlF/C8yLU9XQ==
}
}
来源:https://stackoverflow.com/questions/5591778/c-sharp-get-md5-hash-of-an-embedded-resource-before-extracting-it