Sunday, July 05, 2009

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

Error you might receive while doing File Operation, specially File.Move Operation.

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

Source: mscorlib

Stack Trace: at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.__Error.WinIOError() at System.IO.File.Move(String sourceFileName, String destFileName)

Solution for Error The process cannot access the file because it is being used by another process.

This error might occurs whenever you are trying to upload file, without explicitly removing it from memory.

So ones you remove it from memory possibly this error will get resolved.

File Upload Method 1
protected void btnUploadImages_Click(object sender, EventArgs e)
{
//Get the File Name.
string strPostedFileName = FileUploadImages.PostedFile.FileName;

//Exit if No File Name was entered.
//If file is Zero Length, it is empty or doesn't exist
if (strPostedFileName != string.Empty && FileUploadImages.PostedFile.ContentLength != 0)
{
//Delete Old file before uploading new file.
if (System.IO.File.Exists(strFilePath))
{
System.IO.File.Delete(strFilePath);
}

//Save-Upload File to server.
FileUploadImages.PostedFile.SaveAs(strFilePath);

//Release File from Memory after uploading
FileUploadImages.FileContent.Dispose();
}
}

File Upload Method 2
If you are trying to do something like this
Image bmp = Bitmap.FromFile("c:\Temp\Logo.bmp");
bmp.Save("c:\Temp\Logo.bmp");

Than, replace that with following
Image bmp = Bitmap.FromFile("c:\Temp\Logo.bmp");
bmp.Save("c:\Temp\Logo.bmp");
bmp.Dispose();


File Upload Method 3
FileStream fs = new FileStream(FilePath, FileMode.Create, FileAccess.Write);
fs.Close();
fs.Dispose();

FileStream fs2 = new FileStream(FilePath, FileMode.Open);
fs2.Close();
fs2.Dispose();

6 comments:

Rajesh said...

Your article is very helpful. It covers all the methods. I spent 2 hours to solve this problem and I did it in 5 mins because of your article.

Thank you very much.

Unknown said...

Thanks!

It solved my problem.

girl_withstorytotell said...

what is 'FileUploadImages'????
please reply asap/

girl_withstorytotell said...

what is 'fileuploadimages'??? reply asap.
i tried using dispose()
but i still get the same error.
i also tried disposing the current process by ' Process end=Process.GetCurrentProcess(); end.Dispose();end.Close();
but still getting error

DotNetGuts said...

FileUploadImage is nothing but "Asp.net FileUpload Control"

Unknown said...

Use "Long Path This very useful if you are having problems
in deleting, unlocking, copying and even renaming files.

Most Recent Post

Subscribe Blog via Email

Enter your email address:



Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.
Page copy protected against web site content infringement by Copyscape