被另一个进程使用(在 file.openread() 之后,然后是 .Close())

发布于 2024-10-20 03:28:52 字数 1591 浏览 3 评论 0原文

首先 - 不要看代码就说它太长 它只是看起来那样。

我正在编写一个程序,它将搜索我的计算机并根据文件的 MD5 值删除文件(为了加快速度,我不想搜索所有文件,只搜索那些具有特定文件名的文件)。

我将 FileInfo 发送到名为 ConditionallyDeleteNotWantedFile 的方法,然后它获取该文件的名称并尝试在字典中查找它 - 检索该文件的 MD5 并计算当前的 FileInfo MD5 以查看它们是否相同。 如果是 - 删除该文件。

问题?当我尝试删除时抛出异常...即使没有其他进程使用它。当我尝试使用 Windows 资源管理器删除文件时,它显示 vshost (意思是:VS...)

我缺少什么?

public static bool ConditionallyDeleteNotWantedFile(FileInfo fi)
{
  string 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;
  }
}

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)
{
  string 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;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

↘紸啶 2024-10-27 03:28:52

我不知道这是否是关键,但您在 ComputeHash 中打开流两次,并且有一条路径不会关闭它。我可以建议这个:

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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文