title: C# calculate MD5 for opened file?
body: <p>how I can calculate MD5 hash for a file that is open or used by a process?</p>
  
  <p>the files can be txt or and exe</p>
  
  <p>my current code return error for an exe because it is running</p>
  
  <p>here is my current code</p>
  
  <pre><code>public static string GetMd5HashFromFile(string fileName)
  {
      FileStream file = new FileStream(fileName, FileMode.Open);
      MD5 md5 = new MD5CryptoServiceProvider();
      byte[] retVal = md5.ComputeHash(file);
      file.Close();
  
      StringBuilder sb = new StringBuilder();
      for (int i = 0; i &lt; retVal.Length; i++)
      {
          sb.Append(retVal[i].ToString("x2"));
      }
      return sb.ToString();
  }
  </code></pre>
  
  <p>Cheers.</p>