Using Directory.Exists for UNC path is sloowwwwww

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

When I call Directory.Exists for a UNC path like below...

if (Directory.Exists(@\\SomeServer\C) == false)
{
....
}

and that server does not exist on the connected network, it takes around a
minute to return from the call which properly returns false.

Is there a faster way to determine if a directory or server exists on the
network?
 
When you try to access remote server, network redirector tries to get the
address of the server. It issues DNS query. This process takes a while...

AFAIK there can be several solutions:
- if you know the IP address of the server, try to connect to it on 135-139
( for UNC )
- if you know the IP substitue it into the UNC path.
- if you do not know the IP try to resolve it manually using Dns.Resolve (
..NET 1.0 - 1.1 )
or GetHostEntry ( .NET 2.0 )
 
Vadym,

I don't know the IP. If I use or GetHostEntry() it resolves to a name
without throwing an exception even if the server doesn't exist! If I put

a legit name like:

GetHostEntry("\\MyLocalMachineName")

it returns "MyLocalMachineName.hilton.com" but the IP is 0.0.0.1. (Which is
incorrect I am using a wirless connection at the Hilton hotel and the DHCP
address is 10.252.1.100).

If I use an illegit name, like
GetHostEntry("\\VADYM")

it returns "VADYM.hilton.com" and the IP is also 0.0.0.1.


Am I missing something?
 
GetHostEntry returns the list of ips, did you check the whole list?
smth like

IPHostEntry host;
host = Dns.GetHostEntry(hostname);

Console.WriteLine("GetHostEntry({0}) returns:", hostname);

foreach (IPAddress ip in host.AddressList)
{
Console.WriteLine(" {0}", ip);
}
 
Why are you trying this for a path (server) that doesn't exists in the first
place? Why not ping the server before trying to connect to a remote share
exposed by the server?

Willy.
 
Back
Top