0

Initial situation in Windows 7 (apparently the same in 8.1 and 10):

  1. Create a persistent (!) network drive mapping (a.k.a. "Reconnect at logon").
  2. Now physically disconnect from the network folder (unplug the cable or shut down the target system)
  3. 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.

CodeX
  • 717
  • 7
  • 23
  • You probably need to send a window message of some sort to Explorer to tell it that the network drives have changed. I'm not sure what. Perhaps WM_SETTINGCHANGE? – Harry Johnston Aug 05 '15 at 20:49
  • Well, a workaround like this would work for me as well. However, after a quick test at least WM_SETTINGCHANGE didn't have any effect other than a short flickering. – CodeX Aug 05 '15 at 21:02
  • 2
    You would need to use `SHChangeNotify` to notify Explorer of a change like that - probably the `SHCNE_DRIVEREMOVED` event. – Jonathan Potter Aug 05 '15 at 23:14
  • That sounds promissing and I've tried it right away. In the first moment I thought that this was it, because the drive immediately disappeared in the already open Explorer window. However, hitting F5 or reopening the Explorer showed the drive again. Apparently what this does is visually hide the specified drive, but this doesn't affect the reason it was there in the first place. With that call I can hide all my drives temporarily without any functional effect. – CodeX Aug 06 '15 at 08:22
  • In case there is no other API call that deals with the persistent drive in the Explorer directly, maybe there's a possibility to remove it in the Registry and then update it with SHCNE_DRIVEREMOVED? Interestingly other programs with an own file browser (like FileZilla) don't show that already disconnected drive. It's only visible in the Explorer. – CodeX Aug 06 '15 at 08:26

0 Answers0