Reputation: 75
As noted, I'm on a Windows 8 machine using Visual Studio 2012.
I need to map/unmap network drives as well as get a list of all of the currently mapped drives. To map/unmap I'm using WNetAddConnection2A and WNetCancelConnection2A, respectively, via PInvoke. To get a list of the currently mapped drives, I'm currently using WMI and querying Win32_MappedLogicalDisk.
using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_MappedLogicalDisk"))
{
foreach (ManagementObject queryObj in searcher.Get())
{
}
}
The interesting bit is that the returned "mapped" drives from WMI do not show in the left panel of File Explorer under "Computer." The returned "mapped" drives also do not show using the "net use" command. I can map a drive via WNetAddConnection2A and it will be returned through the Win32_MappedLogicalDisk query but it will not show in File Explorer.
Additionally, I can manually map a drive via File Explorer and the mapped drive will show using "net use" but will not be returned using WMI to query Win32_MappedLogicalDisk.
File Explorer / "net use" don't seem to be talking to the same sources as WNetAddConnection2A / WMI. I've tested this same code on Windows 7 without any issues.
Any help would be much appreciated. Thanks
Upvotes: 0
Views: 1741
Reputation: 136451
Try the Win32_LogicalDisk
WMI class and the DriveType property, (The value 4 indicates a Network Drive).
Select * From Win32_LogicalDisk Where DriveType = 4
Upvotes: 2