简体   繁体   English

枚举断开的网络驱动器-NTFS驱动器格式

[英]Enumerating disconnected network drives - NTFS drive formats

I came across this excellent article that solves the problem of enumerating disconnected drives in C#: Mapped network drives cannot be listed in C# 我碰到了一篇出色的文章,解决了枚举C#中断开连接的驱动器的问题: 映射的网络驱动器无法在C#中列出

The problem is that it seems like this code will NOT include NTFS-formatted drives (only FAT) 问题是似乎此代码将不包括NTFS格式的驱动器(仅FAT)

As I'm not a C++/WinAPI techie, I find it hard to fix the code (if possible at all). 由于我不是C ++ / WinAPI技术人员,因此发现很难修复代码(如果可能的话)。 Any chance anyone already look into it and solved it, or at least give me a hint? 有没有人已经研究并解决了它,或者至少给了我一个提示?

Thanks! 谢谢! Busi 布西

OK, I have an answer. 好,我有一个答案。 It has nothing to do with NTFS and FAT. 它与NTFS和FAT无关。

This is the code I used to enumerate the drives: 这是我用来枚举驱动器的代码:

WNetOpenEnum( RESOURCE_SCOPE.RESOURCE_REMEMBERED, RESOURCE_TYPE.RESOURCETYPE_DISK, 0, resource, out ptrHandle); WNetOpenEnum(RESOURCE_SCOPE.RESOURCE_REMEMBERED,RESOURCE_TYPE.RESOURCETYPE_DISK,0,资源,出自ptrHandle);

Please note the first parameter, RESOURCE_SCOPE.RESOURCE_REMEMBERED. 请注意第一个参数RESOURCE_SCOPE.RESOURCE_REMEMBERED。 This mean that the method will only enumerate those mapped drives that were set as PERSISTED (which means, re-connect at logon). 这意味着该方法将仅枚举那些设置为PERSISTED的映射驱动器(这意味着在登录时重新连接)。

If I change for example the parameter to RESOURCE_SCOPE.RESOURCE_CONNECTED, it will enumerate the non-persisted drives, if they are connected. 例如,如果我将参数更改为RESOURCE_SCOPE.RESOURCE_CONNECTED,则将枚举非持久驱动器(如果已连接)。

If you want all the combinations, you can do: WNetOpenEnum(RESOURCE_SCOPE.RESOURCE_REMEMBERED | RESOURCE_SCOPE.RESOURCE_RECENT | RESOURCE_SCOPE.RESOURCE_CONNECTED, RESOURCE_TYPE.RESOURCETYPE_DISK, 0, resource, out ptrHandle); 如果需要所有组合,则可以执行以下操作:WNetOpenEnum(RESOURCE_SCOPE.RESOURCE_REMEMBERED | RESOURCE_SCOPE.RESOURCE_RECENT | RESOURCE_SCOPE.RESOURCE_CONNECTED,RESOURCE_TYPE.RESOURCETYPE_DISK,0,资源,出自ptrHandle);

Thank you! 谢谢!

If you want to list your local NTFS drives, the article you mentionned won't cut it : the Win32 functions it uses are meant to enumerate network ressources. 如果要列出本地NTFS驱动器,则您所提到的文章不会删减:它使用的Win32函数旨在枚举网络资源。

So if you want to list all local drives and network drives, you have to use both the code from your link, and also call DriveInfo.GetDrives() (or Environment.GetDrives()) from the System.IO namespace. 因此,如果要列出所有本地驱动器和网络驱动器,则必须使用链接中的代码,还必须从System.IO命名空间中调用DriveInfo.GetDrives()(或Environment.GetDrives())。

However, since GetDrives() will also return connected network drives, you need to merge the results of both lists to avoid duplicates : 但是,由于GetDrives()也将返回连接的网络驱动器,因此您需要合并两个列表的结果以避免重复:

    static void Main(string[] args)
    {
        List<string> drives = new List<string>();
        // Assuming you put the API calls in Class1
        foreach (var item in Class1.WNetResource())
        {
            // WNetResource returns the drive letters without a trailing slash
            drives.Add(String.Concat(item.Key, @"\"));
        }

        foreach (var item in Environment.GetLogicalDrives())
        {
            if (!drives.Contains(item))
                drives.Add(item);
        }

        drives.Sort();

        foreach (var drive in drives)
        {
            Console.WriteLine(drive);
        }
    }

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM