Ashish Sharma
Ashish Sharma

Reputation: 387

Fastest way to identify Network Path Exists

In my application I need to search for a network path and do some processing based on the existence of the folder. Let us take an example, I have to search for a path on network and if path exists I have to enable some controls otherwise I need to disable the controls. I tried using DirectoryInfo object and getting the correct values:

    DirectoryInfo dirInfo = new DirectoryInfo(@"\ServerIPAddress\FolderName");

    if(dirInfo.Exists)
    {
            //do something
    }
    else
    {
           //do something else
    }

The problem with the above code is it is consuming more than 2 minutes for getting the Exists property.

Is there any faster way to check for network path existence.

Thanks and regards,
Ashish Sharma

Upvotes: 3

Views: 19945

Answers (2)

close2zero
close2zero

Reputation: 105

There exists a shell API.

It can be used in a following way -

[DllImport("shlwapi.dll")]
private static extern bool PathIsNetworkPath(string pszPath);

Upvotes: 0

Kash
Kash

Reputation: 9039

Usually this would take time only if the folder does not exist in the specified path. You could use a different thread to check the existence of the folder as described here (along with delegates): How to avoid network stalls in GetFileAttributes?

Also you can check this related question: How To: Prevent Timeout When Inspecting Unavailable Network Share - C#

Upvotes: 2

Related Questions