views:

681

answers:

6

Hi,

I use

DirectoryExists (const PathName : String);

to check if a directory is reachable from a computer or not. But if the directory does not exist and the path name is a network path, i.e.

\\computer1\Data

the method takes a very long time to return.

There must be a faster way to determine that a network folder is not reachable. Or can I configure some timeout parameter that DirectoryExists uses internally (I looked at the source code but it just delegates to GetFileAttributes which is defined in kernel32)?

Any ideas?

+5  A: 

If you test for lots of directories you should use threads to do all the queries in parallel because for network shares ther are usually long timeouts.

codymanix
I'm not testing for a lots of directories. Only for one. But DirectoryExists can take about 30 seconds to return, which is annoying.
Smasher
+4  A: 

There was the same question for C#: http://stackoverflow.com/questions/1142080/how-to-avoid-network-stalls-in-getfileattributes

As codymanix said, use threads. The above link will show you how you can do it with delegates in C#. Don't know Delphi, but maybe you know how to convert the code?

JRL
(threads will only help if you can do something in paralel. If your next action is dependant on the share (e.g. for loading config), in the thread case you only gain the ability to show the user some movement, it doesn't speed up )
Marco van de Voort
+13  A: 

There is no faster way:

any function accessing anything on a remote share will timeout when that share is not available.

If the cause of your timeouts is automatic disconnecting of shares, then this link may help you: http://support.microsoft.com/default.aspx?scid=kb;%5BLN%5D;Q138365

If the application can continue without the completion of the check, then you can put the check in a separate thread, and upon completion of the check, you can update your status in the UI.

--jeroen

Jeroen Pluimers
+2  A: 

This is the best way. You could add some code to ping the machine to insure it exists, but this would still leave the routine up to fail as many computers today have software firewalls set up to ignore ping requests, as well as the possibility that the share requested doesn't exist.

Also, on some machines if the UNC path is on the local machine and the local machine does not have an active network card (a wi-fi disconnected laptop for instance in "Airplane" mode) then UNC requests will also fail.

skamradt
A: 

If both computers are on the same domain it will speed-up file operations when dealing with shares.

pani
+1  A: 

In a similar situation like you prescribed, I've added an ICMP ping to the server first. If the server doesn't respond to the ping, I assume it is down. You can decide which timeout to use on the ping yourself, so you can set it much shorter than the timeout used internally when trying to open a file-share.

Stijn Sanders