Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 988
CornelC Avatar asked Sep 01 '14 15:09

CornelC


People also ask

How do I find shared folders on my network?

To find and access a shared folder or printer: Search for Network , and click to open it. Select Search Active Directory at the top of the window; you may need to first select the Network tab on the upper left. From the drop-down menu next to "Find:", select either Printers or Shared Folders.

How do I get a list of all shared folders from all servers?

Computer Management (Local) -> System Tools -> Shared Folders -> Shares. This will show you all the current shares on the system as well as allow you to control them, change permissions, modify access, etc.


2 Answers

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;
    }
like image 158
Atef Sdiri Avatar answered Oct 05 '22 23:10

Atef Sdiri


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.

like image 31
Sellorio Avatar answered Oct 05 '22 21:10

Sellorio