17
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 go to process them.
How can I check if the file is in use?
Today I treat 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....
}
Works using Try{} catch{}, but would like to avoid an exception.
Does anyone know any way to test whether the file is in use without an exception having to be made?