42

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
bramco
  • 467
  • 1
  • 5
  • 6

7 Answers7

142

it may be Garbage Collection issue.

System.GC.Collect(); 
System.GC.WaitForPendingFinalizers(); 
File.Delete(picturePath);
kplshrm7
  • 1,583
  • 2
  • 14
  • 25
29

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();
}
Clemens
  • 123,504
  • 12
  • 155
  • 268
13

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);
Shahid Manzoor
  • 157
  • 1
  • 2
  • apparently this will give exceptions if you try to manipulate, clone or even save the image later, though. – Nyerguds Jan 24 '18 at 12:56
4
var uploadedFile = Request.Files[0]; //Get file
var fileName = Path.GetFileName(uploadedFile.FileName);  //get file name
string fileSavePath = Server.MapPath(fileName); //get path
uploadedFile.SaveAs(fileSavePath); //saving file
FileInfo info = new FileInfo(fileSavePath);//get info file
//the problem ocurred because this, 
FileStream s = new FileStream(fileSavePath, FileMode.Open); //openning stream, them file in use by a process
System.IO.File.Delete(fileSavePath); //Generete a error
//problem solved here...
s.Close();
s.Dispose();
System.IO.File.Delete(fileSavePath); //File deletad sucessfully!
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
-1

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.

Anup Sharma
  • 2,053
  • 20
  • 30
-1

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.

JeffS
  • 327
  • 7
  • 17
-2

In my case, I started a new process of devenv.exe opening a temporary solution file. After the process was ended, I found I could not delete the directory for few minutes. Checking with "resmon", resource monitor, I found it was a executable called PerfWatson2.exe that was using the temp file. Looking at the site, PerfWatson is actually a Visual Studio Customer Experience Improvement Program from MicroSoft. It will lock the file or directory you temporarily used even after you have ended the VS IDE. The solution is to disable the Visual Studio Customer Experience Improvement Program, see this. This shoudln't be an issue after your app is publihsed. But it is quite annoying during debuging.

nips
  • 1
  • 2
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31293051) – Karl Stephen Mar 16 '22 at 12:35