7

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 ?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
CornelC
  • 4,974
  • 1
  • 21
  • 28
  • possible duplicate of [Get a list of all UNC shared folders on a local network server](http://stackoverflow.com/questions/3567063/get-a-list-of-all-unc-shared-folders-on-a-local-network-server) – kamaradclimber Nov 02 '14 at 08:45

4 Answers4

3

I know this thread is old, but this solution might eventually help someone. I used a command line then returned a substring from its output containing the directory names.

    static void Main(string[] args)
    {
        string servername = "my_test_server";
        List<string> dirlist = getDirectories(servername);
        foreach (var dir in dirlist)
        {
            Console.WriteLine(dir.ToString());
        }      
        Console.ReadLine();
    }

    public static List<string> getDirectories (string servername)
    {
        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 \\\\" + servername);
        cmd.StandardInput.Flush();
        cmd.StandardInput.Close();
        string output = cmd.StandardOutput.ReadToEnd();
        cmd.WaitForExit();
        cmd.Close();
        List<string> dirlist = new List<string>();
        if(output.Contains("Disk"))
        {
            int firstindex = output.LastIndexOf("-") + 1;
            int lastindex = output.LastIndexOf("Disk");
            string substring = ((output.Substring(firstindex, lastindex - firstindex)).Replace("Disk", string.Empty).Trim());
            dirlist = substring.Split('\n').ToList();
        }
        return dirlist;
    }
Atef Sdiri
  • 41
  • 6
2

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.

Sellorio
  • 1,806
  • 1
  • 16
  • 32
1

i could not edit or comment on the answer from Sellorio so i made a new answer:

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();
}

if your executation path has a '-' in it, you will recive a zero length error at line:

output = output.Substring(output.LastIndexOf('-') + 2);

fix this by removing the path first:

string CurrentExecutePath = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
output = output.Replace(CurrentExecutePath, "");
output = output.Substring(output.LastIndexOf('-') + 2);
Dave
  • 120
  • 1
  • 10
0

This appears to be a missing part of .net according to codeproject.com. The website nevertheless describes a solution that worked in 2003.

Can you try it and explain if it works ?

kamaradclimber
  • 2,479
  • 1
  • 26
  • 45
  • 1
    This is not what the OP wants. This is to list folders that are shared from your machine. The OP (and I) want to list shared folders from another machine on the network. – Sellorio Jun 15 '17 at 23:58
  • 2
    I really glad to get a comment on an answer from ~3y ago :) – kamaradclimber Jun 16 '17 at 07:16
  • I was looking this up and this was one of the questions that came up. I have now added the solution I found as an answer. – Sellorio Jun 19 '17 at 05:43