C# File.Delete,文件被另一个进程使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/811502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# File.Delete, file being used by another process
提问by Vinzcent
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 :
您的进程是使用 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();
采纳答案by Sadegh
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;
回答by Scott Anderson
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() 方法有什么作用?
回答by Robert Cartaino
First you need to determine if it is yourapplication 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 查看哪个程序打开了特定的文件或目录。它是 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
回答by Rossini
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.
注意:删除了解锁程序的链接 - 包含恶意软件。
回答by SLaks
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.
当您调用Image.FromFile
in 时comboBox3_SelectedIndexChanged
,也许在其他地方也调用时,您不会处理该Image
对象。因此,您的程序正在使用该文件。
You need to Dispose the image every time you open it.
每次打开图像时都需要处理图像。
回答by millimoose
When all else fails, you can use MoveFileExto delete the file on next reboot.
当所有其他方法都失败时,您可以使用MoveFileEx在下次重新启动时删除该文件。
回答by thestar
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 加载的图像保存它们从打开加载的文件。
回答by Sadegh
...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 :@
回答by Deniz
I once used something like thestarand Sadeghposted 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)
您也可以立即删除(新)图片!(如果你想)