Search code examples
c#mapped-drivedriveinfo

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#

The problem is that it seems like this code will NOT include NTFS-formatted drives (only FAT)

As I'm not a C++/WinAPI techie, I find it hard to fix the code (if possible at all). Any chance anyone already look into it and solved it, or at least give me a hint?

Thanks! Busi


Solution

  • OK, I have an answer. It has nothing to do with NTFS and FAT.

    This is the code I used to enumerate the drives:

    WNetOpenEnum( RESOURCE_SCOPE.RESOURCE_REMEMBERED, RESOURCE_TYPE.RESOURCETYPE_DISK, 0, resource, out ptrHandle);

    Please note the first parameter, 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).

    If I change for example the parameter to RESOURCE_SCOPE.RESOURCE_CONNECTED, it will enumerate the non-persisted drives, if they are 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);

    Thank you!