1

I have a function that always creates a directory and put in it some files (images). When the code runs first time, no problem. Second time (always), it gets an error when I have to delete the directory (because I want to recreate it to put in it the images). The error is "The process cannot access the file '...' because it is being used by another process". The only process that access to this files is this function. It's like the function "doesn't leave" the files.

How can I resolve this with a clear solution?

Here a part of the code:

String strPath = Environment.CurrentDirectory.ToString() + "\\sessionPDF";
if (Directory.Exists(strPath))
      Directory.Delete(strPath, true); //Here I get the error
Directory.CreateDirectory(strPath);
//Then I put the files in the directory
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
smn.tino
  • 2,272
  • 4
  • 32
  • 41

5 Answers5

2

If your code or another process is serving up the images, they will be locked for an indefinite amount of time. If it's IIS, they're locked for a short time while being served. I'm not sure about this, but if Explorer is creating thumbs for the images, it may lock the files while it does that. It may be for a split second, but if your code and that process collide, it's a race condition.

Be sure you release your locks when you're done. If the class implements IDisposable, wrap a using statement around it if you're not doing extensive work on that object:

using (var Bitmap = ... || var Stream = ... || var File = ...) { ... }

...which will close the object afterwards and the file will not be locked.

Derreck Dean
  • 3,708
  • 1
  • 26
  • 45
  • Problem solved with using statement. You were right. The problem was that i use: Bitmap myBitmap = new Bitmap(path_of_the_image); I insert this variabile in a using statement like this: using (Bitmap myBitmap = new Bitmap(path_of_the_image)) {...} All works fine. Thanks. – smn.tino Apr 20 '12 at 17:12
1

Just going out on a limb here without seeing the code that dumps the files, but if you're using FileStreams or Bitmap objects, I would double check to ensure you are properly disposing of all of those objects before running the second method.

Steve Danner
  • 21,818
  • 7
  • 41
  • 51
0

The only clear solution on this case is keep track of who is handling access to the directory and fix the bug, by releasing that access.

If the object/resource that handling access is 3rd party, or by any means is not possible to change or access, it's a time to revise an architecture, to handle IO access in a different way.

Hope this helps.

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • Thanks for your notice. Finally i can resolve disposing the objects (Bitmap) that had reference to the images. – smn.tino Apr 20 '12 at 18:11
0

Sounds like you are not releasing the file handle when the file is created. Try doing all of your IO within the using statement, that way the file will be released automatically when you are finished with it.

http://msdn.microsoft.com/en-us/library/yh598w02%28v=vs.80%29.aspx

javram
  • 2,635
  • 1
  • 13
  • 18
0
  • I have seen cases where a virus scanner will scan the new file and prevent the file from being deleted, though that is highly unlikely.

  • Be sure to .Dispose of all IDisposable objects and make sure that nothing has changed your Environment.CurrentDirectory to the directory you want to delete.

agent-j
  • 27,335
  • 5
  • 52
  • 79