IOException 文件被另一个进程使用
我创建了一个函数,可以循环并删除给定目录中的两个相似图像。 (甚至多个目录):
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
文档告诉了您这么多:
因此,您需要在尝试删除图像之前先处理该图像。只需将其保留尽可能短的时间,如下所示:
The documentation tells you as much:
So, you need to dispose of the image before attempting to delete it. Just keep it around for the shortest time possible like so: