?
Solved

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

Posted on 2012-08-27
7
Medium Priority
?
1,902 Views
Last Modified: 2012-09-04
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
0
Comment
Question by:VBdotnet2005
  • 3
  • 3
7 Comments
 
LVL 20

Expert Comment

by:informaniac
ID: 38337242
FileUploadImages.FileContent.Close();
FileUploadImages.FileContent.Dispose();

Open in new window

0
 

Author Comment

by:VBdotnet2005
ID: 38337308
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
 
LVL 20

Expert Comment

by:informaniac
ID: 38337370
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:VBdotnet2005
ID: 38337507
I can delete a file from the location. I did not get any errors.
0
 
LVL 20

Expert Comment

by:informaniac
ID: 38337623
So after manually deleting the file from the location, if you run the code, you still get the error?
0
 

Author Comment

by:VBdotnet2005
ID: 38337793
Well, there is not file to move or delete,there won't be any error. Maybe I am not understanding your question.
0
 
LVL 26

Accepted Solution

by:
Alan Warren earned 2000 total points
ID: 38339975
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Integration Management Part 2
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question