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

标签 c# network-share shared-directory web-folders filesystem-browser

.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 Client Network”,并且出现了已连接和可访问的 WebDAV 文件夹。

问题

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

最佳答案

是的,你可以做到这一点,看看这个网站,它会告诉你如何在 VB 和 C# 中做到这一点

Enumerating Network Drives / Shares

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

关于c# - 如何在 C# 中枚举网络共享和 Web 文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8918477/

相关文章:

c# - 使用可移植类库将东部标准时间字符串转换为 UTC 日期时间

c# - TCP ZeroWindow 问题 C#

python - 为什么从我可以用 Python 读取的目录进行复制时权限被拒绝?

networking - `net use` 启动脚本后网络驱动器进入炼狱

windows - 当中央存储库位于 Windows 文件共享上时,与多个用户一起使用 git 是否安全?

ruby - 共享文件夹前的 Vagrant 预配置

macos - 无法使用 nfs 挂载 vagrant 同步文件夹

google-colaboratory - 在 Colab 上分享 Google Drive 的一部分

C# 重载方法调用与继承

c# - x单元: Assert two List<T> are equal?