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:
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.
Thanks!
It solved my problem.
what is 'FileUploadImages'????
please reply asap/
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
FileUploadImage is nothing but "Asp.net FileUpload Control"
Use "Long Path This very useful if you are having problems
in deleting, unlocking, copying and even renaming files.
Post a Comment