如何在 C# 中枚举网络共享和 Web 文件夹?

发布于 2024-12-27 20:50:27 字数 1192 浏览 5 评论 0 原文

.Net为我们提供了一个FolderBrowserDialog控件来浏览文件夹。然而,这是一个模式对话框。我需要创建一个可以拖放到表单上的用户控件。因此,我一直在考虑创建自己的,我需要在其中获取所有本地驱动器、映射的网络驱动器、UNC 共享和 Web 文件夹(WebDAV/SharePoint)。我使用的是 Windows 7 和 .Net 4.0。

本地和映射网络驱动器
我可以从 DriveInfo.GetDrives() 获取本地驱动器。但是,这些仅向我显示可用/在线的驱动器。在 Windows 资源管理器中,您还可以看到已断开连接/不可用的映射网络驱动器。

网络文件夹/UNC 共享
从我到目前为止发现的情况来看,.Net 中似乎没有枚举 UNC 共享的机制。看来我必须使用 Interop to Win32 API 来使用 WNetOpenEnumWNetEnumResource 函数来枚举网络邻居。我得到了这个工作,但想知道是否没有其他方法。

Web (WebDAV) 文件夹和 SharePoint
在 Windows 资源管理器中,我配置了一些 WebDAV 文件夹。使用与 WNetOpenENumWNetENumResource 上面相同的 Interop 调用,我得到了一个节点“Web 客户端网络”,并且出现了已连接和可访问的 WebDAV 文件夹。

问题

  1. 如何获取已配置但离线的映射网络驱动器?
  2. 是否有另一种方法来枚举 UNC 共享,或者我是否坚持使用上面命名的互操作调用?
  3. WNetEnumResource 返回给我一个“Microsoft Terminal Services”的空节点。如何在不根据英文文本进行过滤的情况下过滤掉这个内容?
  4. WNetEnumResource 返回的Web 文件夹不是我在创建它们时分配给它们的用户友好名称,而是采用IP-Address@Port 的格式,例如\\nnn.nnn。 nnn.nnn@18123。如何获取 Web 文件夹的用户友好名称?
  5. 离线的 Web 文件夹根本没有出现,但在 Windows 资源管理器中却出现了。关于获取离线的有什么建议吗?

.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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞美少年 2025-01-03 20:50:27

是的,您可以执行此操作,请查看此网站,它将向您展示如何在 VB 和 C# 中执行此操作

枚举网络驱动器/共享

这是一个示例应用程序,您也可以从 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));

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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文