title: File is being used by another process after File.Copy
body: <p>I am trying to manage files in my web application. Sometimes, I must create a file in a folder (with File.Copy):</p>
  
  <pre><code>File.Copy(@oldPath, @newPath);
  </code></pre>
  
  <p>And a few seconds later that file may be deleted:</p>
  
  <pre><code>if (File.Exists(@newPath)) {
    File.Delete(@newPath);            
  }
  </code></pre>
  
  <p>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:</p>
  
  <blockquote>
    <p>"The process cannot access the file because it is being used by
    another process."</p>
  </blockquote>
  
  <p>According to the Api, File.Copy don't block the file, does it? </p>
  
  <p>I have tried to release the resources but it hasn't worked. How can I resolve this?</p>
  
  <p><strong>UPDATED:</strong>  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:</p>
  
  <pre><code>  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)) &gt; 0)
        {
          output.Write(buffer, 0, read);
        }
      }
      catch (Exception e)
      {
      }
      finally
      {
        input.Close();
        input.Dispose();
        output.Close();
        output.Dispose();
      }
    }
  </code></pre>