0

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

lpfx
  • 1,476
  • 4
  • 18
  • 40
  • 1
    I do not think it is possible to get a list of shares this way, even though you can browse it in explorer. However, I do not have an answer, because I do not know what API covers this. – John Chadwick Feb 21 '14 at 15:47
  • 1
    There is no `\\Servername` directory. Those are published shares on the remote computer, not file system directories. It might be possible to use WMI or SMB to enumerate the shares, but that's about it. – Luaan Feb 21 '14 at 15:47
  • Actually i thought that i was working with filesystem directories. Am i doing this all wrong? If i am, how can i get all the directories just by passing the servername? – Tiago Lourenço Feb 21 '14 at 15:51
  • I tried use that code but doesn't work. That only show local shares and i want remote "shares". I tried with the management but gives error in access "ACCESS IS DENIED". How can give error in access if I have the impersonate working properly? – Tiago Lourenço Feb 24 '14 at 09:59

1 Answers1

1

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

Community
  • 1
  • 1
peter
  • 14,348
  • 9
  • 62
  • 96
  • What is namespace of ShareCollection! The System.Collections.ReadOnlyCollection doesn't exist in visual studio 2012. I already try Management Class but give me the error "Access is Denied". And i already implement the impersonate – Tiago Lourenço Feb 21 '14 at 17:38
  • You need to download the source code provided on codeproject, there is the definition of `ShareCollection` – peter Feb 21 '14 at 18:48
  • I tried use that code but doesn't work. That only show local shares and i want remote "shares". I tried with the management but gives error in access "ACCESS IS DENIED". How can give error in access if I have the impersonate working properly? – Tiago Lourenço Feb 24 '14 at 09:58