开发者

being used by another process (after a file.openread(), but then .Close())

开发者 https://www.devze.com 2023-02-15 09:36 出处:网络
first of all - don\'t look at the code and say it\'s too long it only looks that way. I\'m writing a program that will search my computer and delete files based on their MD5 value (and to speed thing

first of all - don't look at the code and say it's too long it only looks that way.

I'm writing a program that will search my computer and delete files based on their MD5 value (and to speed things up i don't want to search all the files, just those that have specific file names).

I am sending a FileInfo to a method named ConditionallyDeleteNotWantedFile, it then takes that file's name and trys to find it in the dictionary - retrieve that file's MD5 and computes the current FileInfo MD5 to see if they are the same. If it does - delete the file.

the problem? exception is thrown when i try to delete... even tho no other process uses it. when i try to delete the file using windows explorer it says vshost (meaning:VS...)

what am i missing ?

public static bool ConditionallyDeleteNotWantedFile(FileInfo fi)
{
  s开发者_如何学Pythontring dictMD5;
  if (NotWanted.TryGetValue(fi.Name, out dictMD5))
  {
    string temp = ComputeMD5Hash(fi.FullName);
    // temp will only be null if we couldn't open the file for 
    // read in the md5 calc operation. probably file is in use.
    if (temp == null) 
      return false; 
    if (temp == dictMD5)
    {
      try
      {
        fi.Delete();
      }
      catch { fi.Delete();   // this exception is raised with 
                             // "being used by another process"
      }
      return true;
    }
  }
  return false;
}

public static string ComputeMD5Hash(string fileName)
{
  return ComputeHash(fileName, new MD5CryptoServiceProvider());
}

public static string ComputeHash(string fileName, HashAlgorithm
    hashAlgorithm)
{
  try
  {
    FileStream stmcheck = File.OpenRead(fileName);
    try
    {
      stmcheck = File.OpenRead(fileName);
      byte[] hash = hashAlgorithm.ComputeHash(stmcheck);
      string computed = BitConverter.ToString(hash).Replace("-", "");
      stmcheck.Close();
      return computed;
    }
    finally
    {
      stmcheck.Close();
    }
  }
  catch
  {
    return null;
  }
}


I don't know if that's the key, but you're opening the stream twice in ComputeHash, and there's a path that does not close it. May I suggest this:

public static string ComputeHash(string fileName, HashAlgorithm hashAlgorithm)
{
    string hashFixed = null;
    try
    {
        using (FileStream stmcheck = File.OpenRead(fileName))
        {
            try
            {
                byte[] hash = hashAlgorithm.ComputeHash(stmcheck);
                hashFixed = BitConverter.ToString(hash).Replace("-", "");
            }
            catch
            {
                //logging as needed
            }
            finally
            {
                stmcheck.Close();
            }
        }
    }
    catch
    {
        //logging as needed
    }
    return hashFixed;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消