I have an application that processes a file queue.
I need to open the files for reading and writing.
Sometimes the files are in use when I process them.
How can I check if the file is in use?
Today I deal more or less like this. I created a function:
public bool ArquivoEmUso(string caminhoArquivo)
{
try
{
System.IO.FileStream fs = System.IO.File.OpenWrite(caminhoArquivo);
fs.Close();
return false;
}
catch (System.IO.IOException ex)
{
return true;
}
}
And I use it like this:
if (ArquivoEmUso(@"C:\Teste.txt"))
{
//Processar depois...
}
else
{
//Processar agora....
}
It works by using try {} catch {}, but I'd like to prevent an exception from occurring.
Does anyone know of any way to test if the file is in use without having to throw an exception?