How can I enumerate network shares and web folders in C#?

拜拜、爱过 提交于 2019-12-01 22:10:04

问题


.Net provides us with a FolderBrowserDialog control to browse for folders. This is however a modal dialog box. I need to create a user control that I can drop onto my form. So, I have been looking at creating my own, where I need to get all local drives, mapped network drives, UNC shares and web folders (WebDAV/SharePoint). I am using Windows 7 and .Net 4.0.

Local and Mapped Network Drives
I can get the local drives from DriveInfo.GetDrives(). However, these are only showing me the drives that are available/online. In Windows Explorer, you also see mapped network drives that are disconnected/unavailable.

Network Folders/UNC Shares
From what I have found so far, there does not appear to be a mechanism in .Net to enumerate the UNC shares. It seems that I have to use Interop to Win32 APIs to use the WNetOpenEnum and WNetEnumResource functions to enumerate the network neighborhood. I got this working, but was wondering if there was not another way.

Web (WebDAV) Folders and SharePoint
In Windows Explorer I configured a few WebDAV folders. Using the same Interop calls above WNetOpenENum and WNetENumResource I got a node "Web Client Network" and the WebDAV folders that were connected and accessible appeared.

Questions

  1. How do I get the mapped network drives that are configured, but are offline?
  2. Is there another way to enumerate UNC shares, or am I stuck with using the above named interop calls?
  3. The WNetEnumResource was returning to me an empty node for "Microsoft Terminal Services". How can I filter this out, without filtering based on the English text?
  4. The web folders returned by WNetEnumResource are not the user friendly names I assigned to them when I created them, but are in the format of IP-Address@Port, e.g. \\nnn.nnn.nnn.nnn@18123. How do I get the user friendly names for the web folders?
  5. The web folders that were offline, did not appear at all, yet in Windows Explorer these are appearing. Any suggestions on getting the ones that are offline?

回答1:


yes you can do this take a look at this site and it will show you how to do it in VB as well as C#

Enumerating Network Drives / Shares

here is a sample app you can try as well from codeproject

//

// Enumerate shares on local computer

//

Console.WriteLine("\nShares on local computer:");
ShareCollection shi = ShareCollection.LocalShares;
if (shi != null) 
{
    foreach(Share si in shi) 
    {
        Console.WriteLine("{0}: {1} [{2}]", 
            si.ShareType, si, si.Path);

        // If this is a file-system share, try to

        // list the first five subfolders.

        // NB: If the share is on a removable device,

        // you could get "Not ready" or "Access denied"

        // exceptions.

        if (si.IsFileSystem) 
        {
            try 
            {
                System.IO.DirectoryInfo d = si.Root;
                System.IO.DirectoryInfo[] Flds = d.GetDirectories();
                for (int i=0; i < Flds.Length && i < 5; i++)
                    Console.WriteLine("\t{0} - {1}", i, Flds[i].FullName);

                Console.WriteLine();
            }
            catch (Exception ex) 
            {
                Console.WriteLine("\tError listing {0}:\n\t{1}\n", 
                    si, ex.Message);
            }
        }
    }
}
else
    Console.WriteLine("Unable to enumerate the local shares.");

//

// Resolve local paths to UNC paths.

//

Console.WriteLine("{0} = {1}", 
    fileName, ShareCollection.PathToUnc(fileName));


来源:https://stackoverflow.com/questions/8918477/how-can-i-enumerate-network-shares-and-web-folders-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!