Is there a better way to check if a file is not locked then to open the file to catch an exception. I have a filewatcher running on a directory and I need to do something to the file after the file has completely moved/created in the location. Aren't throwing an exception a performance hit? Is there a better way?
Private Function FileAvailable(ByVal fileName As String) As Boolean
Try
Using inputStream As FileStream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.None)
Return True
End Using
Catch ex As IOException
Return False
End Try
End Function
or
private bool FileAvailable(string fileName) {
try {
using (FileStream inputStream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.None)) {
return true;
}
}
catch (IOException ex) {
return false;
}
}
A little expert advise on the best way. Thanks
The file is being copied into the watch directory. No other user has access to the directory. I just need to verify that the file is completly copied into the directory berfore I process and move the file myself. I need exclusive access to it.