views:

309

answers:

3

Possible Duplicate:
Faster DirectoryExists function?

I want to check if some file exists on a network drive. But FileExists is too slow if drive is disconnected. How can I check it with some timeout?

Examples are welcome.

+3  A: 

Check out

http://stackoverflow.com/questions/1438923/faster-directoryexists-function

I asked the same question there. The essence is that unfortunately you can't avoid those long timeouts if the file does not exist.

Smasher
Thank you, sorry for duplicate
silent
+1  A: 

If you know that when the file exists, FileExists is returning fast, then you could call FileExists from a different thread and use a custom timeout parameter to abort the operation faster.

despart
+1  A: 

The problem is that under the covers FileExists is doing a call to CreateFile (actually NtCreateFile) and the call to CreateFile is synchronous.

You might be able to achieve something by extracting the computer name and pinging the computer name but that only tells you if the NIC associated with the computer is online, it says nothing about the state of the file & print services on the remote machine. Your file might also be on the other side of a DFS connection in which case you can't even determine the correct remote server.

Bottom line is that it's really challenging to do this, you're better off doing what despart said and calling FileExists on another thread. Just make sure that you don't try to clean up the thread when you time out - TerminateThread is an evil API.

Larry Osterman