简体   繁体   English

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

[英]C# File.Delete, file being used by another process

I have a problem when I'm trying to delete an image file.我在尝试删除图像文件时遇到问题。 I always get an error that says: IOExeption was unhandled.我总是收到一条错误消息:IOExeption 未处理。 Acces denied because the file is beining used by another process.访问被拒绝,因为该文件正被另一个进程使用。

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 :您的进程是使用 file 的进程,您需要将图像设置为 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; pcrtrbFoto.Image = img;

img = null;图像 = 空;

GC.Collect(); GC.Collect();

The first area I would look is in your GetPhoto method.我要查看的第一个区域是您的 GetPhoto 方法。 Do you have a StreamReader that hasn't been closed?您是否有尚未关闭的 StreamReader? 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.请确保如果您在删除之前对文件进行任何类型的 I/O,请先关闭这些连接。 What does the GetPhoto() method do? GetPhoto() 方法有什么作用?

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.您可以使用 Mark Russinovich 的 Process Explorer 查看哪个程序打开了特定的文件或目录。 It is part of the Windows Sysinternals line of excellent utilities that every programmer/IT professional should use (or at least be aware of).它是 Windows Sysinternals 优秀实用程序系列的一部分,每个程序员/IT 专业人员都应该使用(或至少知道)。

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

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

When you call Image.FromFile in comboBox3_SelectedIndexChanged , and perhaps elsewhere as well, you don't dispose the Image object.当您在comboBox3_SelectedIndexChanged调用Image.FromFile时,也可能在其他地方调用Image.FromFile时,您不会处理Image对象。 Therefore, your program is keeping the file in use.因此,您的程序正在使用该文件。

You need to Dispose the image every time you open it.每次打开图像时都需要处理图像。

your process is the one that uses file , you need to set image to null use something like this :您的进程是使用 file 的进程,您需要将图像设置为 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;

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

...but, if your application is running on a web hosting plan? ...但是,如果您的应用程序在网络托管计划上运行? You can't run any software in a shared server.您不能在共享服务器中运行任何软件。

I've tried with dispose() and other options, but I can't delete files like Vinzcent.我已经尝试过使用 dispose() 和其他选项,但我无法删除像 Vinzcent 这样的文件。

Maldito IIS :@马尔迪托 IIS :@

This problem occurs when trying to delete / modify a previously saved file from the same software.尝试从同一软件中删除/修改以前保存的文件时会出现此问题。 I solved it by adding this code:我通过添加以下代码解决了它:

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


File.Delete(MyFile);

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.注意:删除了解锁程序的链接 - 包含恶意软件。

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我曾经使用过类似thestarSadegh发布的内容,但在某些情况下它不起作用/有帮助,所以我找到了一个不同的解决方案,我已经在这里发布了

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) (如果你想)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 File.Delete被另一个进程使用 - File.Delete being used by another process File.Delete无法删除文件,因为它正在被另一个进程使用 - File.Delete cannot delete a file because it is being used by another process File.Delete无法删除文件,因为它正在被另一个进程使用。 不确定如何解决此问题 - File.Delete cannot delete a file because it is being used by another process. Not sure how to fix this File.Delete进程无法访问文件,因为它正在被另一个进程使用 - File.Delete the process cannot access the file because it is being used by another process File.Delete错误“该进程无法访问该文件,因为该文件正在被另一个进程使用” - File.Delete error “The process cannot access the file because it is being used by another process” c#删除文件失败,正在被另一个进程使用 - c# failed delete file, it is being used by another process C#PDF删除 - 文件'被另一个进程使用' - C# PDF Delete - File 'being used by another process' 位图上的File.Delete不起作用,因为该文件正在被另一个进程使用(我使用了bitmap.Dispose();) - File.Delete on bitmap doesn't work because the file is being used by another process (I used bitmap.Dispose();) 其他进程正在使用的C#文件 - C# File being used by another process 另一个进程正在使用C#文件 - C# file is being used by another process
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM