Initial situation in Windows 7 (apparently the same in 8.1 and 10):
- Create a persistent (!) network drive mapping (a.k.a. "Reconnect at logon").
- Now physically disconnect from the network folder (unplug the cable or shut down the target system)
- Log off and on again. A balloon message will tell "Could not reconnect all network drives", but the drive will still be visible in the Explorer (with a red cross).
At this point I'd like to remove that network drive by code from my application.
For this I'm using WNetCancelConnection2 (which works fine with any other situation), but to make it easier for you to reproduce you can also use the command line. Run the command:
net use Z: /delete
(or whatever drive letter you've used). It'll tell you that it was completed successfully. Running "net use" will show no network drives connected. HOWEVER, in the Explorer the drive is still there! Rightclicking it now and choosing to disconnect will result in an error. To remove that drive one has to log off and on again or to kill explorer.exe and run it again.
Interestingly, if instead of removing the drive by code or command line you right-click it in the explorer and disconnect it from there it works flawlessly! So that right-click option does something differently than "net use" and the mentioned API call. I'd really like to know what it does.
So what's the problem here? And how to solve it? Any solution in any programming language would be fine. Thanks in advance.