How to get a list of all shared folders?

S

sva

Using C# for an application in which I am working on, I need to display the
available shared folders on the computer that's running the application.
The shared folder paths need to be in UNC format (i.e., \\servername\share).
I don't need to read, write, edit, delete, etc. any files or folders, I
simply need to get a list of all the shared folders and get their full UNC
path.

I know the System.IO namespace has some methods and/or properties to get
directories, but I don't know how to limit this to only the available shared
folders.

Thanks!
 
W

Willy Denoyette [MVP]

| Using C# for an application in which I am working on, I need to display
the
| available shared folders on the computer that's running the application.
| The shared folder paths need to be in UNC format (i.e.,
\\servername\share).
| I don't need to read, write, edit, delete, etc. any files or folders, I
| simply need to get a list of all the shared folders and get their full UNC
| path.
|
| I know the System.IO namespace has some methods and/or properties to get
| directories, but I don't know how to limit this to only the available
shared
| folders.
|
| Thanks!
|
|

I'm not entirely clear on what you are after, are you trying to get a list
of exported shared folders (server side), or are you looking for the mapped
network connections (client side).
Anyway both can be obtained using System.Management and WMI.
Following is a sample illustrates how to get the UNC paths of the exported
shares...

using(ManagementClass exportedShares = new
ManagementClass("Win32_Share" ))
using(ManagementClass computer = new
ManagementClass("Win32_computersystem" ))
{
string localSystem = null;
ManagementObjectCollection localComputer = computer.GetInstances();
foreach(ManagementObject mo in localComputer)
{
localSystem = mo["Name"].ToString();
}
ManagementObjectCollection shares = exportedShares.GetInstances();
foreach(ManagementObject share in shares)
// dump UNC path
Console.WriteLine(@"UNC path \\{0}\{1}", localSystem, share["Name"]);
}

Willy.
 
S

sva

Thanks! I'm only trying to get a list of exported shared folders (server
side), and your solution works great.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top