Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a list of all UNC shared folders on a local network server

Tags:

c#

.net

I'm trying to get a list of all shared folders available on a local intranet server.

The System.IO.Directory.GetDirectories() works fine for a path like \\myServer\myShare, however I'm getting an exception for a path like \\myServer:

Unhandled Exception: System.ArgumentException: The UNC path should be of the form \server\share.

Is there a way to get a list all shared folders for a server? Ultimately I'm looking for a method that can handle both scenarios based on a given path - returning a list of all shares for a given server and returning a list of all subdirectories for a given network shared folder.

like image 371
Misha Narinsky Avatar asked Aug 25 '10 14:08

Misha Narinsky


People also ask

How can I see all network shares on my network?

Use the MS-DOS "net share" command Click Start, Run, type cmd, and press Enter . At the MS-DOS prompt, type net share and press Enter . Each of the shares, the location of the resource, and any remarks for that share are displayed.

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 find shared folders in Windows Server?

Open Windows File Explorer, go to the Folders pane, and select Network. Select the computer that has the shared folders you want to browse. In older versions of Windows, open Entire Network and select Microsoft Windows Network to see shares.


3 Answers

Here's a technique that uses System.Management (add a reference to this assembly):

using (ManagementClass shares = new ManagementClass(@"\\NameOfTheRemoteComputer\root\cimv2", "Win32_Share", new ObjectGetOptions())) {
    foreach (ManagementObject share in shares.GetInstances()) {
        Console.WriteLine(share["Name"]);
    }
}

Appropriate permissions are required.

like image 180
Bradley Smith Avatar answered Sep 19 '22 10:09

Bradley Smith


I think this is what you are looking for http://www.codeproject.com/KB/IP/networkshares.aspx

like image 37
ajay_whiz Avatar answered Sep 18 '22 10:09

ajay_whiz


    private DataTable GetSharedFolderAccessRule()
    {
        DataTable DT = new DataTable();

        try
        {
            DT.Columns.Add("ShareName");
            DT.Columns.Add("Caption");
            DT.Columns.Add("Path");
            DT.Columns.Add("Domain");
            DT.Columns.Add("User");
            DT.Columns.Add("AccessMask");
            DT.Columns.Add("AceType");

            ManagementScope Scope = new ManagementScope(@"\\.\root\cimv2");
            Scope.Connect();
            ObjectQuery Query = new ObjectQuery("SELECT * FROM Win32_LogicalShareSecuritySetting");
            ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);
            ManagementObjectCollection QueryCollection = Searcher.Get();

            foreach (ManagementObject SharedFolder in QueryCollection)
            {
                {
                    String ShareName = (String) SharedFolder["Name"];
                    String Caption   = (String)SharedFolder["Caption"];
                    String LocalPath = String.Empty;
                    ManagementObjectSearcher Win32Share = new ManagementObjectSearcher("SELECT Path FROM Win32_share WHERE Name = '" + ShareName + "'");
                    foreach (ManagementObject ShareData in Win32Share.Get())
                    {
                        LocalPath = (String) ShareData["Path"];
                    }

                    ManagementBaseObject Method = SharedFolder.InvokeMethod("GetSecurityDescriptor", null, new InvokeMethodOptions());
                    ManagementBaseObject Descriptor = (ManagementBaseObject)Method["Descriptor"];
                    ManagementBaseObject[] DACL = (ManagementBaseObject[])Descriptor["DACL"];
                    foreach (ManagementBaseObject ACE in DACL)
                    {
                        ManagementBaseObject Trustee = (ManagementBaseObject)ACE["Trustee"];

                        // Full Access = 2032127, Modify = 1245631, Read Write = 118009, Read Only = 1179817
                        DataRow Row = DT.NewRow();
                        Row["ShareName"]  = ShareName;
                        Row["Caption"]    = Caption;
                        Row["Path"]       = LocalPath;
                        Row["Domain"]     = (String) Trustee["Domain"];
                        Row["User"]       = (String) Trustee["Name"];
                        Row["AccessMask"] = (UInt32) ACE["AccessMask"];
                        Row["AceType"]    = (UInt32) ACE["AceType"];
                        DT.Rows.Add(Row);
                        DT.AcceptChanges();
                    }
                }
            }
        }
        catch (Exception ex) 
        {
            MessageBox.Show(ex.StackTrace, ex.Message);
        }

        return DT;
    }
like image 34
KyungSeoc Jang Avatar answered Sep 21 '22 10:09

KyungSeoc Jang