0

I am trying to upload a file in asp.net. File may be image or pdf. If the file already exist then I have to remove existing file and upload the new file. But if I try to delete existing file, it shows an error that "The process cannot access the file because it is being used by another process" This is the code for my file upload.

if (FileUploadFollowUpUN.HasFile)
{
    if (Request.QueryString.Count > 0 && Request.QueryString["PCD"] != null)
    {
        filename = System.IO.Path.GetFileName(FileUploadFollowUpUN.FileName.Replace(FileUploadFollowUpUN.FileName, Request.QueryString["PCD"] + " " + "D" + Path.GetExtension(FileUploadFollowUpUN.FileName)));
        SaveFilePath = Server.MapPath("~\\ECG\\") + filename;
        DirectoryInfo oDirectoryInfo = new DirectoryInfo(Server.MapPath("~\\ECG\\"));
        if (!oDirectoryInfo.Exists)
            Directory.CreateDirectory(Server.MapPath("~\\ECG\\"));

        if (File.Exists(SaveFilePath))
        {
            File.SetAttributes(SaveFilePath, FileAttributes.Normal);

            File.Delete(SaveFilePath);
        }
        FileUploadFollowUpUN.SaveAs(Server.MapPath(this.UploadFolderPath) + filename);
        Session["FileNameFollowUpUN"] = filename;
        if (System.IO.Path.GetExtension(FileUploadFollowUpUN.FileName) == ".pdf")
        {
            imgPhoto.ImageUrl = "~/Images/pdf.jpg";
            ZoomImage.ImageUrl = "~/Images/pdf.jpg";
            imgPhoto.Enabled = true;
        }
        else
        {
            imgPhoto.ImageUrl = "~/ECG/" + filename;
            imgPhoto.Enabled = true;
            ZoomImage.ImageUrl = "~/ECG/" + filename;
        }
    }
}

How can I get rid out of this error?

Erwin
  • 4,757
  • 3
  • 31
  • 41
Microsoft Developer
  • 5,229
  • 22
  • 93
  • 142
  • 2
    Well, it would seem that some other process is using the file! You should try to find the process responsible (e.g. the webserver). Windows won't let you delete a file if it is in use. – nneonneo Aug 24 '12 at 07:36
  • search for the error in the code reading these files ( if you can acceed it ) you probably forgot to close the file after reading. – Felice Pollano Aug 24 '12 at 07:38
  • How can I find that process? As such there is no process using this file. – Microsoft Developer Aug 24 '12 at 07:38
  • 1
    Assuming that the existing file was also uploaded in the same way, it's entirely possible that your service is still "using" the file. Make sure you close the file once you have uploaded it. – verdesmarald Aug 24 '12 at 07:39
  • Can you please tell me how to close the file as I am not using any stream writer – Microsoft Developer Aug 24 '12 at 07:44
  • I couldn't tell you since I have no idea how your `SaveAs` method is implemented. It might help if you add the contents that method to the question. – verdesmarald Aug 24 '12 at 07:48
  • check this question, how-does-one-figure-out-what-process-locked-a-file http://stackoverflow.com/questions/860656/how-does-one-figure-out-what-process-locked-a-file-using-c – Clinton Ward Aug 24 '12 at 08:09
  • @veredesmarald SaveAs is a method of FileUploadClass and not user defined method – Microsoft Developer Aug 24 '12 at 09:28
  • I can't see anything wrong with that code. Try using the advice [here](http://stackoverflow.com/questions/7484604/how-can-i-find-out-what-process-is-using-my-file) to discover what process is using your file. It's often either antivirus or windows search. – Jude Fisher Aug 24 '12 at 07:38

1 Answers1

0

There is a similar question here on how to find what process is using a file

You should try to dispose any file methods before trying to delete.

You could stick it in a while loop if you have something which will block until the file is accessible

  public static bool IsFileReady(String sFilename)
    {
        // If the file can be opened for exclusive access it means that the file
        // is no longer locked by another process.
        try
        {
            using (FileStream inputStream = File.Open(sFilename, FileMode.Open, FileAccess.Read, FileShare.None))
            {
                if (inputStream.Length > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }
        }
        catch (Exception)
        {
            return false;
        }
    }
Community
  • 1
  • 1
Clinton Ward
  • 2,441
  • 1
  • 22
  • 26