Delete a file being used by another process

bramco picture bramco · Nov 7, 2012 · Viewed 77.6k times · Source

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?

Answer

kplshrm7 picture kplshrm7 · Jan 15, 2014

it may be Garbage Collection issue.

System.GC.Collect(); 
System.GC.WaitForPendingFinalizers(); 
File.Delete(picturePath);