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

时间:2009-05-01 14:06:16

标签: c# error-handling

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

我不知道可能是什么过程以及如何解决它。

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

}

谢谢, Vinzcent


感谢所有人的帮助。

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


你的进程是使用文件的进程,你需要将image设置为null使用类似这样的东西:

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的();

10 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分:3)

首先,您需要确定它是您的应用程序还是打开文件的其他应用程序。

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

您可以在此处获取:http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx

答案 2 :(得分:3)

您将使用缩略图:

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加载的图像会保存从开放加载的文件。

答案 3 :(得分:2)

当您在Image.FromFile中呼叫comboBox3_SelectedIndexChanged时,也可能在其他地方呼叫时,您不会处置Image对象。因此,您的程序正在使用该文件。

每次打开图像时都需要处理图像。

答案 4 :(得分:2)

您的进程是使用文件的进程,您需要将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;

答案 5 :(得分:1)

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

答案 6 :(得分:0)

您可以使用Unlocker程序告诉您哪些程序已锁定文件

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

答案 7 :(得分:0)

...但是,如果您的应用程序在网络托管计划上运行?您无法在共享服务器中运行任何软件。

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

Maldito IIS:@

答案 8 :(得分:0)

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

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


File.Delete(MyFile);

答案 9 :(得分:-1)

我曾经使用过像 thestar Sadegh 这样的内容 但在某些情况下,它没有工作/帮助所以我找到了一个不同的解决方案 我已经发布了here

仍然在这里代码(你可以在看完链接和问题后更好地理解它):

   var imageAsByteArray = File.ReadAllBytes(imagePath);

   // I use as example a pictureBox:
   pictureBox1.Image = byteArrayToImage(imageAsByteArray);

   // Or/and safe/copy/replace it:
   File.WriteAllBytes(picture_Path, imageAsByteArray);

您也可以立即删除(新)图片! (如果你想)

相关问题