To avoid exceptions like
(1) Process cannot access the file because it was used by another process
I used the following method to test the accessibility of the file before any further processing.
private bool CheckIfFileBeingUsed(string FilePath)
{
FileStream Fs = null;
try
{
Fs = File.Open(FilePath, FileMode.Open, FileAccess.Read, FileShare.None);
Fs.Close();
}
catch (Exception)
{
return true; //Error File is being used
}
return false; //File is not being used.
}
Could anyone advise me there's any Windows API or other solutions for such testing of file accessibility instead of the above File.Open Method ?