13

I am trying to manage files in my web application. Sometimes, I must create a file in a folder (with File.Copy):

File.Copy(@oldPath, @newPath);

And a few seconds later that file may be deleted:

if (File.Exists(@newPath)) {
  File.Delete(@newPath);            
}

However, I don't know why the new file remains blocked by the server process (IIS, w3wp.exe) after File.Copy. After File.Delete I get the exception:

"The process cannot access the file because it is being used by another process."

According to the Api, File.Copy don't block the file, does it?

I have tried to release the resources but it hasn't worked. How can I resolve this?

UPDATED: Indeed, using Process Explorer the file is blocked by IIS process. I have tried to implement the copy code in order to release manually the resources but the problem still goes on:

  public void copy(String oldPath, String newPath)
  {
    FileStream input = null;
    FileStream output = null;
    try
    {
      input = new FileStream(oldPath, FileMode.Open);
      output = new FileStream(newPath, FileMode.Create, FileAccess.ReadWrite);

      byte[] buffer = new byte[32768];
      int read;
      while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
      {
        output.Write(buffer, 0, read);
      }
    }
    catch (Exception e)
    {
    }
    finally
    {
      input.Close();
      input.Dispose();
      output.Close();
      output.Dispose();
    }
  }
Cœur
  • 37,241
  • 25
  • 195
  • 267
jbernal
  • 785
  • 1
  • 14
  • 29
  • 1
    So what is using the file? Clearly you wouldn't just pointlessly create a file which nothing else uses only to delete it a few seconds later, so something must be using it. What is that something? – Matthew Watson Aug 06 '13 at 08:11
  • The file comes from another system so, it is changed in it I must replace the old one. I am developing a "mirror" system. – jbernal Aug 06 '13 at 08:13
  • Perhaps this similar question might help you: http://stackoverflow.com/questions/7937966/ – Bridge Aug 06 '13 at 08:14
  • 1
    @Bridge `File.Copy()` will always complete and close the file handle before it returns. – Matthew Watson Aug 06 '13 at 08:15
  • Are you using threads ? – Tan Aug 06 '13 at 08:17
  • @Tan No. File.Copy and File.Delete are executed as a consecuence of different http requests. – jbernal Aug 06 '13 at 08:18
  • Maybe the virus scanner scans the file, which can cause problems. i had a similar problem on a desktop pc and tried a "catch three times then raise" strategy which worked well. – Christian Sauer Aug 06 '13 at 08:22
  • @Christian Sauer I thought about it but If I reboot the IIS process then File.Delete works properly. – jbernal Aug 06 '13 at 08:23
  • 1
    @jbernal: Maybe there is still a handle on the file somewhere? Do you process the file in any way? Maybe that object hangs still in memory? Does the blockade last forever, without restarting the IIS? – Christian Sauer Aug 06 '13 at 08:28
  • @Christian Sauer If there is a process working with file I can't find it. I have killed some processes (antivirus included) but nothing resolved the problem. Only when I kill w3wp.exe process. Thanks anyway. – jbernal Aug 06 '13 at 08:38
  • Have you verified that you're not accessing the file in other places, apart from copying and deleting? – Lasse V. Karlsen Aug 06 '13 at 09:42
  • @Lasse V. Karlsen Yes, with Process Explorer you can know what processes are using my file but none apart my IIS. – jbernal Aug 06 '13 at 09:48
  • No, what I meant was, no other piece of code in your web application? Are you somewhere else opening the file for reading, and not closing it, or similar? Are those places (copy/delete) the *only* places in your code that you access that file? – Lasse V. Karlsen Aug 06 '13 at 10:00
  • Correct, It is! I didn't expect but an image rendering in the next lines were causing the blockade. – jbernal Aug 06 '13 at 11:22

3 Answers3

5

You can try Process Explorer to find which application opened the file handle. If Process Explorer cannot find that, use Process Monitor to trace which process is trying to access the file.

aaron cheung
  • 532
  • 2
  • 10
2

This can be caused by file indexers or anti virus software, which typically scan all new files.

x5657
  • 1,172
  • 2
  • 13
  • 26
  • 1
    Of course, it is an option but how many time does it take? I have waited for several minutes but the file continues being blocked. – jbernal Aug 06 '13 at 08:43
  • 1
    You could try [Process Explorer](http://technet.microsoft.com/en-gb/sysinternals/bb896653.aspx) from sysinternals. Using the "Find handle or DLL" feature will show you which processes have an open handle on your file. – x5657 Aug 06 '13 at 08:57
  • 1
    Very useful program. I have used it and indeed the only process who uses the file is w3wp.exe (IIS). I am going to implement the copy function with streams in order to write explicitly the close() or dispose() sentence. – jbernal Aug 06 '13 at 09:09
0

File was being blocked by another process without me being aware of it. Process Explorer was really helpful.

Typical easy issue hard to spot.

jbernal
  • 785
  • 1
  • 14
  • 29