2

I have written a function that deletes a file from it's location. The code can be seen as below:

if (File.Exists(strPath + "/FLV/" + flvvideoname))
{
    File.Delete(strPath + "/FLV/" + flvvideoname);
}

'strPath' is the path to the directory. When the code get's executed, the file doesn't always get deleted. Running the same code over and over will delete the file. When I troubleshoot this issue, the break point doesn't go past the file delete line and I don't see any errors either. If the break point makes it past the file delete line, that means the file has been deleted properly.

How do I resolve this issue?

Owen Pauling
  • 11,349
  • 20
  • 53
  • 64
Ahmed Mujtaba
  • 2,110
  • 5
  • 36
  • 67

2 Answers2

2

The handle from file creation can sometimes be not freed. Try calling

GC.Collect();
GC.WaitForPendingFinalizers();

before deleting.

Also check out this SO question: Delete a file being used by another process.

EDIT:

Alternatively to awoid calling GC.Collect() (for reasons pointed out by Boas Enker), you may want to wait for the file in a loop - like this.

Community
  • 1
  • 1
Piotr Falkowski
  • 1,957
  • 2
  • 16
  • 24
  • 1
    Not a good idea. GC.Collect has a huge impact on the whole runtime. also it doesn't solve the problem and makes the system less predictable – Boas Enkler Nov 13 '15 at 10:55
  • @BoasEnkler I have tried this method and it worked. Are there any alternatives? – Ahmed Mujtaba Nov 13 '15 at 10:59
  • 1
    @AhmedMujtaba There are some dirty, like using Thread.Sleep(), but you do not wont to go this way. – Piotr Falkowski Nov 13 '15 at 11:12
  • The question is why is the file locked. is there more code accessign that file ? are you flushing and disposing other actors that access the file? – Boas Enkler Nov 13 '15 at 11:55
  • @BoasEnkler The answer to you qustion is, that while creating the file (File.Create), the file handle is sometimes not released (finalized) before the delete call. Check it out, with just Thread.Sleep(2000); it should work too, but is nondeterministic and dirty. GC class is just for this sort of stuff. – Piotr Falkowski Nov 13 '15 at 12:15
  • 1
    I disagree, I would more like to check in a loop wether the action was successfull instead of calling gc.collect. In small prototype application it isn't important but when having a normal - big sizd application and you have gc.collect in such a "detail" event which may be recurring a lot of time then you could just mess up the whole garbage collection.... see forexample http://stackoverflow.com/questions/118633/whats-so-wrong-about-using-gc-collect Retrying the action (if nothing else possible) would only have a local effect and would not negativly influence the whole runtime – Boas Enkler Nov 13 '15 at 12:23
  • Thank you both for your help. I have used the loop and it works. @PiotrFalkowski – Ahmed Mujtaba Nov 13 '15 at 13:10
0

I'm sure that problem not in File deletion. May be when you are retrieving Filename from database, or setting some variable dynamically, you do this not in the first page load, but even at postbacks. So if you are retrieving filename dynamically, do the next step:

if (!IsPostBack){//function for retrieving data from database

//your_variable = some_value; }

Khazratbek
  • 1,656
  • 2
  • 10
  • 17