Q:

List all shared folders from a network location

I want to list all the shared directories from a network server. 
To list directories from a shared network directory I used 
Directory. GetDirectories(@"\\server\share\")

The problem is I want to list all folders on \\server. 
If I use the same method I get an exception saying 

The UNC path should be of the form \server\share

I looked all over the place and I can't find a solution for this.  
Does anybody have any idea of what I should do in order to display the folders in \\share ? 

A:

The best solution I could find was to call "net" app from a hidden cmd.exe instance:
public static string[] GetDirectoriesInNetworkLocation(string networkLocationRootPath)
{
    Process cmd = new Process();
    cmd. StartInfo. FileName = "cmd.exe";
    cmd. StartInfo. RedirectStandardInput = true;
    cmd. StartInfo. RedirectStandardOutput = true;
    cmd. StartInfo. CreateNoWindow = true;
    cmd. StartInfo. UseShellExecute = false;
    cmd. StartInfo. WindowStyle = ProcessWindowStyle. Hidden;
    cmd. Start();
    cmd. StandardInput. WriteLine($"net view {networkLocationRootPath}");
    cmd. StandardInput. Flush();
    cmd. StandardInput. Close();

    string output = cmd. StandardOutput. ReadToEnd();

    cmd. WaitForExit();
    cmd. Close();

    output = output. Substring(output. LastIndexOf('-') + 2);
    output = output. Substring(0, output. IndexOf("The command completed successfully.")); 

    return
        output
            .Split(new[] { '\r', '\n' }, StringSplitOptions. RemoveEmptyEntries)
            .Select(x => System. IO.Path. Combine(networkLocationRootPath, x.Substring(0, x.IndexOf(' '))))
            .ToArray();
}

Depending on your use case, you may want to validate networkLocationRootPath to avoid any cmd injection issues.