IOException 文件被另一个进程使用

发布于 2024-11-11 13:44:10 字数 1879 浏览 9 评论 0原文

我创建了一个函数,可以循环并删除给定目录中的两个相似图像。 (甚至多个目录):

public void DeleteDoubles()
    {
        SHA512CryptoServiceProvider sha1 = new SHA512CryptoServiceProvider();
        string[] images = Directory.GetFiles(@"C:\" + "Gifs");
        string[] sha1codes = new string[images.Length];
        GifImages[] Gifs = new GifImages[images.Length];

        for (int i = 0; i < images.Length; i++)
        {
            sha1.ComputeHash(GifImages.ImageToByteArray(Image.FromFile(images[i])));
            sha1codes[i] = Convert.ToBase64String(sha1.Hash);
            Gifs[i] = new GifImages(images[i], sha1codes[i]);
        }

        ArrayList distinctsha1codes = new ArrayList();
        foreach (string sha1code in sha1codes)
            if (!distinctsha1codes.Contains(sha1code))
                distinctsha1codes.Add(sha1code);

        for (int i = 0; i < distinctsha1codes.Count; i++)
            if (distinctsha1codes.Contains(Gifs[i].Sha1Code))
            {
                for (int j = 0; j < distinctsha1codes.Count; j++)
                    if (distinctsha1codes[j] != null && distinctsha1codes[j].ToString() == Gifs[i].Sha1Code)
                    {
                        distinctsha1codes[j] = Gifs[i] = null;
                        break;
                    }
            }

        try
        {
            for (int i = 0; i < Gifs.Length; i++)
                if (Gifs[i] != null)
                    File.Delete(Gifs[i].Location);
        }
        catch (IOException)
        {
        }
    }

问题是,在我留下要删除的文件列表后,我无法删除它们,因为我得到“System.IO.IOException 文件正在被另一个进程使用。 。”

我尝试使用 procexp 来查看哪些进程正在使用我的文件,这些文件似乎正在被 MyApplication.vshost.exe 使用。它开始使用该行上的文件:

sha1.ComputeHash(GifImages.ImageToByteArray(Image.FromFile(images[i])));

含义 Image.FromFile(images[i]) 打开文件但从不关闭它。

I've made a function that cycles and deletes two similar images in a given directory. (or even multiple directories) :

public void DeleteDoubles()
    {
        SHA512CryptoServiceProvider sha1 = new SHA512CryptoServiceProvider();
        string[] images = Directory.GetFiles(@"C:\" + "Gifs");
        string[] sha1codes = new string[images.Length];
        GifImages[] Gifs = new GifImages[images.Length];

        for (int i = 0; i < images.Length; i++)
        {
            sha1.ComputeHash(GifImages.ImageToByteArray(Image.FromFile(images[i])));
            sha1codes[i] = Convert.ToBase64String(sha1.Hash);
            Gifs[i] = new GifImages(images[i], sha1codes[i]);
        }

        ArrayList distinctsha1codes = new ArrayList();
        foreach (string sha1code in sha1codes)
            if (!distinctsha1codes.Contains(sha1code))
                distinctsha1codes.Add(sha1code);

        for (int i = 0; i < distinctsha1codes.Count; i++)
            if (distinctsha1codes.Contains(Gifs[i].Sha1Code))
            {
                for (int j = 0; j < distinctsha1codes.Count; j++)
                    if (distinctsha1codes[j] != null && distinctsha1codes[j].ToString() == Gifs[i].Sha1Code)
                    {
                        distinctsha1codes[j] = Gifs[i] = null;
                        break;
                    }
            }

        try
        {
            for (int i = 0; i < Gifs.Length; i++)
                if (Gifs[i] != null)
                    File.Delete(Gifs[i].Location);
        }
        catch (IOException)
        {
        }
    }

The problem is that after I'm left with the list of files I want to delete, I can't delete them because I get "System.IO.IOException file is being used by another process..."

I tried using procexp to see which processes are using my files and it seems the files are being used by MyApplication.vshost.exe. It starts using the file on that line :

sha1.ComputeHash(GifImages.ImageToByteArray(Image.FromFile(images[i])));

Meaning Image.FromFile(images[i]) opens the file but never closes it.

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

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

发布评论

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

评论(1

故乡的云 2024-11-18 13:44:10

文档告诉了您这么多:

在图像被处理之前,文件将保持锁定状态。

因此,您需要在尝试删除图像之前先处理该图像。只需将其保留尽可能短的时间,如下所示:

for (int i = 0; i < images.Length; i++)
{
    using( var img = Image.FromFile( images[i] ) )
    {
        sha1.ComputeHash(imageToByteArray(img));
    }

    sha1codes[i] = Convert.ToBase64String(sha1.Hash);
    Gifs[i] = new GifImages(images[i], sha1codes[i]);
}

The documentation tells you as much:

The file remains locked until the Image is disposed.

So, you need to dispose of the image before attempting to delete it. Just keep it around for the shortest time possible like so:

for (int i = 0; i < images.Length; i++)
{
    using( var img = Image.FromFile( images[i] ) )
    {
        sha1.ComputeHash(imageToByteArray(img));
    }

    sha1codes[i] = Convert.ToBase64String(sha1.Hash);
    Gifs[i] = new GifImages(images[i], sha1codes[i]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文