28 November 2008

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

The process cannot access the file because it is being used by another process.
This error message is mostly comes up,
when you try to access a file which is opened by another process.

You may open an image file in one of your form in a picturebox with using ImageFromFile or something.
I mostly use memorystream to open an image.
After read it in byte[] write it into a stream and close it.

You can check whether a file is being used or not with a simple function.
Try to open a file with none share.
 public bool IsFileUsedbyAnotherProcess(string filename)
{
try
{
File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.None);
}
catch (System.IO.IOException exp)
{
return true;
}
return false;

}
This function will return true if
the file because it is being used by another process or not.

8 comments:

Re@lBanda said...

hey can you tell me how u using memory stream to load an image to memory.. I'm using two images. I'll compare them and if they are same I'll delete them. I've used FromFile method to get images. it worked. but after compared and when they are same I tried to delete them using this coding

string zDirectoryL = "C:\\Eagle Eye";
string zFileNameL = "image1.jpg";
FileInfo TheFileInfo = new FileInfo(zDirectoryL + "\\" + zFileNameL);
if (TheFileInfo.Exists)
{
File.Delete(zDirectoryL + "\\" + zFileNameL);
}

but it gives "The process cannot access the file because it is being used by another process". So I'm trying to get these images to memory and delete them after compared. so this won't be using the images then I can delete them.

Abdul Aziz Farooqi said...

Hi dude,
You should create a fake loop for thread, I mean you have to let all thread wait until you thread finish your works, so given while loop will help you to accomplish your task.


bool _FileUse = false;
while (!_FileUse)
{
try
{
StreamWriter strWriter = new StreamWriter(p_directoryPath + p_Filename + ".txt",true);
strWriter.Write(_Builder);
strWriter.AutoFlush = true;
strWriter.Flush();
strWriter.Dispose();
strWriter.Close();
Thread.Sleep(1000);
_FileUse = true;
}
catch (Exception ex)
{
Thread.Sleep(1000);
_FileUse = false;
}

this loop will provide flag to thread to wiat or check.

Abdul Aziz Farooqi said...

bool _FileUse = false;
while (!_FileUse)
{
try
{
StreamWriter strWriter = new StreamWriter(p_directoryPath + p_Filename + ".txt",true);
strWriter.Write(_Builder);
strWriter.AutoFlush = true;
strWriter.Flush();
strWriter.Dispose();
strWriter.Close();
Thread.Sleep(1000);
_FileUse = true;
}
catch (Exception ex)
{
Thread.Sleep(1000);
_FileUse = false;
}
hope this works , this loop will create a flag and let all threads to wait for a prticular thread. if you do not get the logic email me i will send you the entire code which you requires :0

Anonymous said...

Besides checking to see if it's being used, even when it is, it might be possible to open it using FileShare.ReadWrite:

File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

morteza said...

IOException may occure for some other exceptions too! it's not only "The process cannot access the file because it is being used by another process"

If you want to be specific you need to check the error message to see if it contains "The process cannot access the file because it is being used by another process"

Anonymous said...

Your code is not working correctly, you have to close the opened FileStream...

private static bool IsFileUsedbyAnotherProcess(string filename)
{
try
{
using (FileStream fs = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.None))
{

}
}
catch (System.IO.IOException exp)
{
return true;
}
return false;

}

srinivas said...

i had used GC.Collect();
and GC.WaitForPendingFinalizers();
it worked is there any risk involved using Garbage collector

srinivas dev said...

i had used GC.Collect();
GC.WaitForPendingFinalizers();
worked for me. is there any risk involving using GC