title: Most efficient way to compare a memorystream to a file C# .NET
body: <p>I have a MemoryStream containing the bytes of a PNG-encoded image, and want to check if there is an exact duplicate of that image data in a directory on disk. The first obvious step is to only look for files that match the exact length, but after this I'd like to know what's the most efficient way to compare the memory against the files. I'm not very experienced working with streams.</p>
  
  <p>I had a couple thoughts on the matter:</p>
  
  <p>First, if I could get a hash code for the file, it would (presumably) be more efficient to compare hash codes rather than every byte of the image. Similarly, I could compare just some of the bytes of the image, giving a "close-enough" answer.</p>
  
  <p>And then of course I could just compare the entire stream, but I don't know how quick that would be.</p>
  
  <p>What's the best way to compare a MemoryStream to a file? Byte-by-byte in a for-loop?</p>