error :The process cannot access the file because it is being used by another process

I am trying to do the same and still got the same error. I delete the old file and created a new file. Please help.

FileUploadImages.FileContent.Dispose();  < this does not help.

http://dotnetguts.blogspot.com/2009/07/process-cannot-access-file-because-it.html
VBdotnet2005Asked:
Who is Participating?
 
Alan WarrenApplications DeveloperCommented:
Hi VBdotnet2005,
Something like this may help:
	string strFileName = "";
	string strSavePath = "";
	string strSaveDir = "";
	bool bolOkToSave = 0;

	// 1... Before attempting to save the file, verify
	// that the FileUpload control contains a file.
	if (FileUpload1.HasFile)
	{

	  // 2... get the uploaded filename and create save-paths
	  strFileName = FileUpload1.FileName;

	  strSaveDir = System.Web.HttpContext.Current.Server.MapPath("~/") + "img\\tmp\\"; // this path should exist on your server
	  strSavePath = strSaveDir + strFileName; // returns http://localhost/mysite/img/tmp/thefile.jpg


	  if (System.IO.File.Exists(strSavePath))
	  {
		try
		{
		  System.IO.File.Delete(strSavePath);
		}
		catch (Exception ex)
		{
		  // handle any errors that occurred
		  // Console.WriteLine(ex)
		  try // again
		  {
			System.IO.File.Delete(strSavePath);
		  }
		  finally
		  {
			// do nothing, it failed for some reason
		  }
		}
	  }

	  // check again to see if the file was deleted
	  if (System.IO.File.Exists(strSavePath))
	  {
		bolOkToSave = false;
	  }

	  if (bolOkToSave)
	  {
		// 3... Save the uploaded file to the tmp dir
		FileUpload1.SaveAs(strSavePath);
	  }

Open in new window

Alan
0
 
Obadiah ChristopherCommented:
FileUploadImages.FileContent.Close();
FileUploadImages.FileContent.Dispose();

Open in new window

0
 
VBdotnet2005Author Commented:
I am still getting the same error

System.IO.IOException: The process cannot access the file 'my_to_my_file\test_2.xlsx' because it is being used by another process.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.File.Delete(String path)
   at LossMit_Excel_Upload.btnUpload_Click(Object sender, EventArgs e)
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
Obadiah ChristopherCommented:
Can you go and manually delete the file from the location once and then try again.

If you get an error while manually trying to delete the file, kill the w3p.exe process and try again.
0
 
VBdotnet2005Author Commented:
I can delete a file from the location. I did not get any errors.
0
 
Obadiah ChristopherCommented:
So after manually deleting the file from the location, if you run the code, you still get the error?
0
 
VBdotnet2005Author Commented:
Well, there is not file to move or delete,there won't be any error. Maybe I am not understanding your question.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.