该进程无法访问文件xxxxxx,因为它正由另一个进程使用 [英] The process cannot access the file xxxxxx because it is being used by another process

查看:205
本文介绍了该进程无法访问文件xxxxxx,因为它正由另一个进程使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VS2010编写一个WinForm应用程序& VS2012与.Net4。



在应用程序中,我将单个图像加载到图片框中,然后删除用户不想保留的图像。



我每次尝试删除这些不需要的图像时都会收到以上错误消息:

进程无法访问文件xxxxxx,因为它正被另一个进程使用



以下用于将图像加载到动态创建的图片框中以及删除不需要图像的代码。



选项1:

加载图片:

I am writing a WinForm application using VS2010 & VS2012 with .Net4.

In the application I load individual images into pictureboxes and then delete those images the user does not wish to keep.

Everytime I try to delete these "unwanted" images I get the above error message:
"The process cannot access the file xxxxxx because it is being used by another process"

The following is used to load an image into a dynamically created picturebox together with the code to delete the unwanted images.

Option 1:
Load Image:

picBox.Image = Image.FromFile(imgInfo.FullName).GetThumbnailImage(128, 128, Nothing, Nothing)



删除图片:


Delete Image:

For Each imgFile As String In Directory.GetFiles(imgSharedFolder)
   File.Delete(imgFile)
Next imgFile





选项2:

由于将图像文件加载到图片框中会锁定图像文件我通过将图像文件读入FileStream然后在将图像加载到图片框中后关闭FileStream来尝试以下内容



加载:



Option 2:
Since loading an image file into a picturebox will "lock" the image file I tried the following by reading the image file into a FileStream and then Close the FileStream after loading the image into the picturebox

Load:

fs = New System.IO.FileStream(imgInfo.FullName, IO.FileMode.Open, IO.FileAccess.Read)
picBox.Image = System.Drawing.Image.FromStream(fs).GetThumbnailImage(128, 128, Nothing, Nothing)
fs.Close()



删除:


Delete:

Dim picList As String() = Directory.GetFiles(imgSharedFolder, "*.jpg")
For Each f As String In picList
   File.Delete(f)
Next f



我得到了相同的错误信息。



选项3:

经过一些搜索,阅读和尝试后,我遇到了这个建议来创建一个图像对象和然后在图像加载到图片框后处理图像对象:



加载:


I get the same error message.

Option 3:
After some more searching, reading and trying I came across this suggestion to create an image object and then Dispose of the image object once the image is loaded into the picturebox:

Load:

Dim newImage As Image
newImage = Image.FromFile(imgInfo.FullName).GetThumbnailImage(128, 128, Nothing, Nothing)
picBox.Image = newImage





不幸的是我收到了同样的错误信息。



有没有其他可能解决这个问题的方法,我可能忽略了什么?



Unfortunately I just got the same error message.

Is there any other possible solution to this problem, something I may have overlooked ?

推荐答案

Image.FromFile和Image.FromStream要求文件/流可用于从它们创建的图像的生命 - 因此,它们保持文件句柄直到它们处理图像。



最简单的方法是加载图像,将其复制到新图像,然后处理原始图像。这是我用于C#的代码:

Image.FromFile and Image.FromStream require that the file / stream are available for the life of the Image that is created from them - as a result, they hold the file handle until they Image is Disposed.

The easiest way to do it is to load the image, copy it into a new Image, and then Dispose the original. This is the code I use for C#:
/// <summary>
/// Get an Image without locking the file.
/// </summary>
/// <remarks>
/// Image.FromFile and Image.FromStream have problems in that the first
/// locks the file until the Image is Disposed, and teh latter requires
/// the stream to be opena nd available until the Image is Disposed, which
/// effectively locks the file as well.
/// This doesn't, because it uses a copy of the image.
/// </remarks>
/// <param name="path"></param>
/// <returns></returns>
public static Image GetImage(string path)
    {
    Image img;
    using (Image temp = Image.FromFile(path))
        {
        img = new Bitmap(temp);
        }
    return img;
    }



不幸的是,我使用的在线翻译目前没有回复,所以...


Unfortunately the online translator I use isn't responding at the moment, so...

Dim img As Image
Dim tmp as Image = Image.FromFile(path)
img = new BitMap(tmp)
tmp.Dispose()

应该这样做,但是没有经过测试。

Should do it, but it's not tested.


我有一个类似的问题这是我需要在图片框控件中显示图像后删除图像文件。

它始终与框架3.5一起使用然后成为框架4.0的问题

我们解决了这个问题通过在将图片框图像设置为空之后强制进行垃圾收集

示例:

picPhoto.Image = Nothing

picPhoto.Refresh()

GC.Collect()
I had a similar issue to this where I needed to delete an image file after displaying the image in a picturebox control.
It always worked with framework 3.5 then became an issue with framework 4.0
We resolved this by forcing the garbage collection after setting the picturebox image to nothing
Example:
picPhoto.Image = Nothing
picPhoto.Refresh()
GC.Collect()


这篇关于该进程无法访问文件xxxxxx,因为它正由另一个进程使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆