Get directories of remote Server

Tiago Lourenço picture Tiago Lourenço · Feb 21, 2014 · Viewed 9.5k times · Source

I'm trying to get all directories of a remote server.

For example: path: "\\Servename\folder" - it works!

path: "\\Servename" - error

I tried this:

DirectoryInfo dir = new DirectoryInfo (@"\\SERVERNAME"); <- Error happens here
//Get Directories from \\SERVERNAME
DirectoryInfo[] dirInfos = dir.GetDirectories();

Error: ERROR: The UNC path should be of the form \\server\share

Answer

peter picture peter · Feb 21, 2014

The reason is that you need to read available shares and not just the directories, as the comments answered.

However getting the list of all available shares on a server is a bit more complicated, it was already answered in a different question: Get a list of all UNC shared folders on a local network server

The most voted answer was Network Shares and UNC paths