C#File.Delete,另一进程正在使用该文件

C#File.Delete,另一进程正在使用该文件,c#,error-handling,C#,Error Handling,我在尝试删除图像文件时遇到问题。 我总是收到一个错误,上面写着:ioexecption未处理。访问被拒绝,因为文件正被另一个进程使用 我不知道会是什么过程,怎么解决 private void comboBox3_SelectedIndexChanged(object sender, EventArgs e) { Album album = GetAlbum(comboBox1.SelectedIndex);

我在尝试删除图像文件时遇到问题。 我总是收到一个错误,上面写着:ioexecption未处理。访问被拒绝,因为文件正被另一个进程使用

我不知道会是什么过程,怎么解决

private void comboBox3_SelectedIndexChanged(object sender, EventArgs e) { Album album = GetAlbum(comboBox1.SelectedIndex); Photo photo = GetPhoto(comboBox1.SelectedIndex, comboBox3.SelectedIndex); txtPhotoPath.Text = Directory.GetCurrentDirectory() + "\\" + photo.SPath; lblExtention.Text = photo.SExtention; txtPhotoTitle.Text = photo.STitle; pctrbFoto.Image = Image.FromFile(foto.SPath).GetThumbnailImage(GetWitdth(photo.SPath, GetHeight(photo.SPath, 150)), GetfHeight(photo.SPath, 150), null, new IntPtr()); } private void btnChangePhoto_Click(object sender, EventArgs e) { Album album = GetAlbum(comboBox1.SelectedIndex); Photo photo = GetPhoto(comboBox1.SelectedIndex, comboBox3.SelectedIndex); File.Delete("Albums\\Images\\" + photo.STitle + foto.SExtention); photo.SExtention = lblExtention.Text; photo.STitle = txtPhotoTitel.Text; Photo.SPath = txtPath.Text; File.Copy(photo.SPath, "Albums\\Images\\" + photo.STitle + photo.SExtention); } private void comboBox3\u SelectedIndexChanged(对象发送方,事件参数e) { Album Album=GetAlbum(comboBox1.SelectedIndex); Photo Photo=GetPhoto(comboBox1.SelectedIndex,Combox3.SelectedIndex); txtpotopath.Text=Directory.GetCurrentDirectory()+“\\”+photo.SPath; lblExtention.Text=photo.SExtention; txtPhotoTitle.Text=photo.STitle; pctrbFoto.Image=Image.FromFile(foto.SPath).GetThumbnailImage(GetWitdth(photo.SPath,GetHeight(photo.SPath,150)),GetfHeight(photo.SPath,150),null,new IntPtr(); } 私有void btnChangePhoto_单击(对象发送者,事件参数e) { Album Album=GetAlbum(comboBox1.SelectedIndex); Photo Photo=GetPhoto(comboBox1.SelectedIndex,Combox3.SelectedIndex); 文件.Delete(“相册\\图像\\”+photo.STitle+foto.SExtention); photo.SExtention=lblExtention.Text; photo.STitle=txtPhotoTitel.Text; Photo.SPath=txtPath.Text; 文件.Copy(photo.SPath,“相册\\Images\\”+photo.STitle+photo.SExtention); } 谢谢, 文森特


谢谢大家的帮助

我用过这个,现在效果很好



您的进程是使用文件的进程,您需要将image设置为null,并使用如下内容:

using(var img = Image.FromFile(foto.SPath).GetThumbnailImage(GetWitdth(photo.SPath, GetHeight(photo.SPath, 150)), GetfHeight(photo.SPath, 150), null, new IntPtr()))
  pctrbFoto.Image = img;
var img=Image.FromFile(foto.SPath).GetThumbnailImage(GetWitdth(photo.SPath), GetHeight(photo.SPath,150)),GetfHeight(photo.SPath,150),null,new IntPtr()

pctrbFoto.Image=img

img=null


GC.Collect()

我要查看的第一个区域是您的GetPhoto方法。您是否有尚未关闭的StreamReader?如果在删除之前对文件执行任何类型的I/O操作,请确保首先关闭这些连接。GetPhoto()方法的作用是什么?

首先,您需要确定是您的应用程序还是另一个打开了文件的应用程序

您可以使用Mark Russinovich的Process Explorer查看哪个程序打开了特定的文件或目录。它是Windows系统内部优秀实用程序系列的一部分,每个程序员/It专业人员都应该使用(或至少应该知道)


您可以在这里获得它:

您可以使用解锁程序来告诉您哪些程序锁定了文件


注意:删除了解锁程序的链接-包含恶意软件。

当您调用
Image.FromFile
中的
组合框3\u SelectedIndexChanged
,或者在其他地方,您不处理
Image
对象。因此,您的程序正在使用该文件


每次打开映像时,您都需要对其进行处理。

当所有其他操作都失败时,您可以使用在下次重新启动时删除该文件。

在获取缩略图的位置使用:

using(Image img = Image.FromFile(foto.SPath))
{
  pctrbPhoto. Image = img.GetThumbnailImage(
  GetWitdth(photo.SPath, GetHeight(photo.SPath, 150)), 
  GetfHeight(photo.SPath, 150), null, new IntPtr()); 
}
而是确保源映像在使用完毕后被释放(关闭)

按照您的方式,从文件加载的图像会一直保留,直到垃圾收集器决定释放它为止,这可能需要一段时间


使用FromFile加载的图像保留从打开的位置加载的文件。

您的进程就是使用该文件的进程,您需要将图像设置为null 使用类似以下内容:

using(var img = Image.FromFile(foto.SPath).GetThumbnailImage(GetWitdth(photo.SPath, GetHeight(photo.SPath, 150)), GetfHeight(photo.SPath, 150), null, new IntPtr()))
  pctrbFoto.Image = img;

…但是,如果您的应用程序运行在web托管计划上?您不能在共享服务器中运行任何软件

我尝试过dispose()和其他选项,但无法删除像Vinzcent这样的文件


Maldito IIS:@

我曾经用过类似于thestarSadegh的东西 但在某些情况下,它不起作用,所以我找到了不同的解决方案 我已经发了

代码仍在这里(在观看链接和问题后,您可能会更好地理解它):


您还可以立即删除(新)图片!(如果需要)

尝试从同一软件中删除/修改以前保存的文件时,会出现此问题。 我通过添加以下代码解决了此问题:

System.GC.Collect(); 
System.GC.WaitForPendingFinalizers();


File.Delete(MyFile);

一般来说,调用GC.Collect不是一个好主意:因为Image实现了IDisposable,所以应该调用img.Dispose(),或者(最好)使用“using”块。您还可以使用Process Explorer告诉您哪些程序锁定了文件(),我同意。但我看不到显示程序正在使用的文件的选项。虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接供参考。如果链接页面因双重提示而更改,则仅链接答案可能无效(呵呵:)