Search code examples
c#hashmd5embedded-resource

c# get md5 hash of an embedded resource before extracting it


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


Solution

  • 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.