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

时间:2022-08-17 07:19:37

I have a problem when I'm trying to delete an image file. I always get an error that says: IOExeption was unhandled. Acces denied because the file is beining used by another process.

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

I do'nt know what process that could be and how to solve it.

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

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

}

Thanks, Vinzcent


Thanks to all for the help.

感谢大家的帮助。

I used this and it works very well now

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


your process is the one that uses file , you need to set image to null use something like this :

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

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

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

pctrbFoto.Image = img;

pctrbFoto.Image = img;

img = null;

img = null;

GC.Collect();

9 个解决方案

#1


your process is the one that uses file , you need to set image to null use something like this :

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

#2


The first area I would look is in your GetPhoto method. Do you have a StreamReader that hasn't been closed? Make sure that if you're doing any sort of I/O on the file prior to the delete that you close those connections first. What does the GetPhoto() method do?

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

#3


where you are getting the thumbnail use:

你在哪里使用缩略图:

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

instead to ensure that the source image is disposed (closed) when you are finished with it.

相反,确保在完成源图像时处置(关闭)源图像。

They way you have it, the image that is loaded from the file sticks around until the garbage collector decides to release it, which might be some time.

他们拥有它,从文件加载的图像会一直存在,直到垃圾收集器决定释放它,这可能是一段时间。

Images loaded with FromFile hold the file that they were loaded from open.

使用FromFile加载的图像会保存打开时加载的文件。

#4


First you need to determine if it is your application or another application that has the file open.

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

You can use Process Explorer by Mark Russinovich to see which program has a particular file or directory open. It is part of the Windows Sysinternals line of excellent utilities that every programmer/IT professional should use (or at least be aware of).

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

You can get it here: http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx

你可以在这里得到它:http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx

#5


When you call Image.FromFile in comboBox3_SelectedIndexChanged, and perhaps elsewhere as well, you don't dispose the Image object. Therefore, your program is keeping the file in use.

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

You need to Dispose the image every time you open it.

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

#6


When all else fails, you can use MoveFileEx to delete the file on next reboot.

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

#7


you can use the Unlocker program to tell you what program(s) have the file locked

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

Note: Removed link to Unlocker program - contains malware.

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

#8


...but, if your application is running on a web hosting plan? You can't run any software in a shared server.

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

I've tried with dispose() and other options, but I can't delete files like Vinzcent.

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

Maldito IIS :@

马尔多托IIS:@

#9


I once used something like thestar and Sadegh posted but for some cases it didnt work/helped so I found a diffrent solution I already postet it here

我曾经使用类似thestar和Sadegh的东西,但在某些情况下它没有工作/帮助所以我发现了一个不同的解决方案我已经在这里发布

still here the code (u may understand it better after watching the link and the question):

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

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

You also can delete the (new) picture instantly ! (if you want)

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

#1


your process is the one that uses file , you need to set image to null use something like this :

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

#2


The first area I would look is in your GetPhoto method. Do you have a StreamReader that hasn't been closed? Make sure that if you're doing any sort of I/O on the file prior to the delete that you close those connections first. What does the GetPhoto() method do?

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

#3


where you are getting the thumbnail use:

你在哪里使用缩略图:

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

instead to ensure that the source image is disposed (closed) when you are finished with it.

相反,确保在完成源图像时处置(关闭)源图像。

They way you have it, the image that is loaded from the file sticks around until the garbage collector decides to release it, which might be some time.

他们拥有它,从文件加载的图像会一直存在,直到垃圾收集器决定释放它,这可能是一段时间。

Images loaded with FromFile hold the file that they were loaded from open.

使用FromFile加载的图像会保存打开时加载的文件。

#4


First you need to determine if it is your application or another application that has the file open.

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

You can use Process Explorer by Mark Russinovich to see which program has a particular file or directory open. It is part of the Windows Sysinternals line of excellent utilities that every programmer/IT professional should use (or at least be aware of).

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

You can get it here: http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx

你可以在这里得到它:http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx

#5


When you call Image.FromFile in comboBox3_SelectedIndexChanged, and perhaps elsewhere as well, you don't dispose the Image object. Therefore, your program is keeping the file in use.

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

You need to Dispose the image every time you open it.

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

#6


When all else fails, you can use MoveFileEx to delete the file on next reboot.

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

#7


you can use the Unlocker program to tell you what program(s) have the file locked

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

Note: Removed link to Unlocker program - contains malware.

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

#8


...but, if your application is running on a web hosting plan? You can't run any software in a shared server.

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

I've tried with dispose() and other options, but I can't delete files like Vinzcent.

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

Maldito IIS :@

马尔多托IIS:@

#9


I once used something like thestar and Sadegh posted but for some cases it didnt work/helped so I found a diffrent solution I already postet it here

我曾经使用类似thestar和Sadegh的东西,但在某些情况下它没有工作/帮助所以我发现了一个不同的解决方案我已经在这里发布

still here the code (u may understand it better after watching the link and the question):

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

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

You also can delete the (new) picture instantly ! (if you want)

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