Delete a file being used by another process

2019-01-01 09:18发布

I'm trying to programmically delete a file, but the file is apparently being used by another process (which happens to be my program). Basically, the program loads images from a folder by using FromUri to create a Bitmap, which is then loaded into an Image array, which in turn becomes the child of a stackpanel. Not very efficient, but it works.

I've tried clearing the stackpanel's children, and making the images in the array null, but I'm still getting the IOException telling me that the file is being used by another process.

Is there some other way to remove the file from my application's processes?

7条回答
回忆,回不去的记忆
2楼-- · 2019-01-01 09:35

Another way is to delete file. Load your file using FileStream class and release an file through stream.Dispose(); it will never give you the Exception "The process cannot access the file '' because it is being used by another process."

using (FileStream stream = new FileStream("test.jpg", FileMode.Open, FileAccess.Read))
{
    pictureBox1.Image = Image.FromStream(stream);
     stream.Dispose();
}

 // delete your file.

 File.Delete(delpath);
查看更多
孤独寂梦人
3楼-- · 2019-01-01 09:36

it may be Garbage Collection issue.

System.GC.Collect(); 
System.GC.WaitForPendingFinalizers(); 
File.Delete(picturePath);
查看更多
像晚风撩人
4楼-- · 2019-01-01 09:46

In order to release an image file after loading, you have to create your images by setting the BitmapCacheOption.OnLoad flag. One way to do this would be this:

string filename = ...
BitmapImage image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = new Uri(filename);
image.EndInit();

Although setting BitmapCacheOption.OnLoad works on a BitmapImage that is loaded from a local file Uri, this is afaik nowhere documented. Therefore a probably better or safer way is to load the image from a FileStream, by setting the StreamSource property instead of UriSource:

string filename = ...
BitmapImage image = new BitmapImage();

using (var stream = File.OpenRead(filename))
{
    image.BeginInit();
    image.CacheOption = BitmapCacheOption.OnLoad;
    image.StreamSource = stream;
    image.EndInit();
}
查看更多
高级女魔头
5楼-- · 2019-01-01 09:46

I had the same issue. The problem I had was with the openFileDialog and saveFileDialog having the following set:

MyDialog.AutoUpgradeEnabled = false;

I commented out that line and it was resolved.

查看更多
残风、尘缘若梦
6楼-- · 2019-01-01 09:56

from my point of view, the general answer would be to backtrack the object that is keeping an open process with the file you want to delete. In my case it was a MailMessage, but just as well it might be a thread, filestream, etc, which you need to dispose.

查看更多
后来的你喜欢了谁
7楼-- · 2019-01-01 09:58

I had the similar issue. The only difference was that I was using Binding(MVVM Pattern). Nothing much worked then I removed everything and tried with Binding Mode=OneWay along with GC.Collect() before calling File.Delete(path) and it worked finally.

查看更多
登录 后发表回答