Click here to Skip to main content
14,699,089 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi,
am getting error The process cannot access the file because it is being used by another process when uploading file to the folder.when am working on visualstudio its wroking but its giving the above error when am copied the application into IIS
this is my code

if (FileUpload1.HasFile)
        {


            try
            {


               string folder_from_config = ConfigurationManager.AppSettings["FileUploadFolder"].ToString();
               if (System.IO.File.Exists(Server.MapPath(folder_from_config) + "\\" + System.IO.Path.GetFileName(FileUpload1.FileName)))
               {

                   System.Windows.Forms.MessageBox.Show("File already exists. Do you want to overwrite");
               }

                FileUpload1.SaveAs(Server.MapPath(folder_from_config) + "\\" + System.IO.Path.GetFileName(FileUpload1.FileName));
                LabelUpload.Text = FileUpload1.FileName;
                Session["Input_Data_Excel_File"] = Server.MapPath((folder_from_config) + "\\" + System.IO.Path.GetFileName(FileUpload1.FileName));

            }

            catch (Exception ex)
            {

                errormsg.Text += "ERROR: " + ex.Message.ToString();
            }
Posted
Updated 12-Sep-20 17:30pm
v2

hi,

important thing to note that we have to always close the connections after reading the Excel Workbook to avoid the following error

The process cannot access the file '\Path\; because it is being used by another process.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IO.IOException: The process cannot access the file '\Path\; because it is being used by another process.
   
Read through this[^]. Is is most likely a permissions issue.
   
Comments
#realJSOP 5-Dec-11 10:26am
   
I would think that if this were the case, he'd get a completely different exception.
The file is already (exclusively) open by another process. What are you trying to upload Web.Config, or something like that?
   
v2
Comments
fjdiewornncalwe 5-Dec-11 10:28am
   
Doesn't only have to be something like the web.config. I have encountered this when deploying to a server while a call is being made to the application, in which case the assemblies themselves are locked as well.
Another issue with your code...

if (System.IO.File.Exists(Server.MapPath(folder_from_config) + "\\" + System.IO.Path.GetFileName(FileUpload1.FileName)))
   {

       System.Windows.Forms.MessageBox.Show("File already exists. Do you want to overwrite");
   }


You shouldn't be using System.Windows.Forms.MessageBox in an ASP.Net application. Once that is deployed onto a web server, the client who uploads the file wont see the message box

You are performing a POST operation when sending a file - If you want the client to see some some of message, you have to send a Response. You can't just use a MessageBox!

You could possibly register a javascript to run on the client, using the Page.ClientScript.RegisterStartupScript syntax
   
Comments
pradeep manne 6-Dec-11 4:18am
   
hi, i have comment that message box still am getting the error,when i upload for first time its working properly
The process cannot access the file because it is being used by another process
The most possible chance might the file your uploading is already open or in use. You can open the task manager and check for the file. You can close any other that might have access to the file and try uploading again.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900