简体   繁体   中英

Can't map or unmap drive because windows thinks it's in use?

I wrote a class which allows me to programmatically map and unmap network drives. In my case there are two folders which i map to two drives (Y: and Z:) It worked fine but something happened and I cannot map or unmap Y: drive anymore.

The exception i get is

Multiple connections to a server or shared resource by the same user, using more than one user name, are not allowed. Disconnect all previous connections to the server or shared resource and try again

i had a look in the command line with

  net use

which resulted in

 Status       Local     Remote                    Network
 ---------------------------------------------------------------------------
 OK           U:        \\ramsay\Users            Microsoft Windows Network
 OK           Z:        \\imperial\EzyCSV         Microsoft Windows Network

I even tried to map/unmap the drive via the command line but no success i get the same error message.

I used as a starting point aejw's network class which can be found here: http://www.codeproject.com/Articles/6847/Map-Network-Drive-API

and this is the code which i am using to map / unmap:

   public static void Map(string address, string drive)
   {
       NetworkDrive nd = new NetworkDrive
       {
           Persistent = true,
           Force = true,
           PromptForCredentials = false,
           SaveCredentials = true,
           ShareName = address,
           LocalDrive = drive
       };
       try
       {
           nd.MapDrive(@"domain\username", "password");
       }
       catch (Exception err)
       {
           MessageBox.Show("Cannot map drive!\nError: " + err.Message);
       }

   }

   public static void Unmap( string drive)
   {
       NetworkDrive nd = new NetworkDrive
       {
           Force = true,
           LocalDrive = drive
       };
       try
       {
           nd.UnMapDrive();
       }
       catch (Exception err)
       {
           MessageBox.Show("Cannot unmap drive!\nError: " + err.Message);
       }
   }

the strange thing is that the same code works for unmapping and mapping the Z drive with that path but not more for Y:

is there any other way i can force a disconnection of Y: or some sort of cache and locked file which tells windows that it is already used but it shows nowhere?

today I started with a fresh mind and thought I test if the problem still occurs, if it does i'll remove all the drives and try to troubleshoot.

For my surprising i was able to fix the problem. The drive and share were not listed anywhere as described above, the commands to mount the share drive failed with the same message above, unmounting failed with the message that the drive is not being used etc. rebooting did not change anything in the situation

For some reason a

net use /delete *

released the network resource even though everything else failed.

So simple that it is almost embarrassing. I am still not sure how this has happened and why there was no indication that the drive/share was being used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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