title: File upload in ASP.NET - How can I prevent exceptions?
body: <p>I have a FileUploader control in my web form. If the file being uploaded is already present, I want to delete it, and overwrite it with the newly-uploaded file. But I get an error, as the file is in use by another process, and thus the application can't delete it. Sample code:</p>
  
  <pre><code>if (FUpload.HasFile)
  {
      string FileName = Path.GetFileName(FUpload.PostedFile.FileName);
      string Extension = Path.GetExtension(FUpload.PostedFile.FileName);
      string FolderPath = ConfigurationManager.AppSettings["FolderPath"];
      string FilePath = Server.MapPath(FolderPath + FileName);
      if (File.Exists(FilePath))
      {
          File.Delete(FilePath);
      }
      FUpload.SaveAs(FilePath);
  }
  </code></pre>
  
  <p>Is there anything I can do apart from writing the code in <code>try</code>/<code>catch</code> blocks?</p>