1

I'm using the following lines below to create a connection to a shared network location, but the problem is with any connections active (I think), network.MapNetworkDrive("..") will throw an error:

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 got pass this error by using net use * /delete from the command line, but is there an equivalent commands in C#?

IWshNetwork_Class network = new IWshNetwork_Class();
network.MapNetworkDrive("z:", @shared_path, Type.Missing, "Admin", "!QAZxsw2");


...

network.RemoveNetworkDrive("z:");          
nathanchere
  • 8,008
  • 15
  • 65
  • 86
jerryh91
  • 1,777
  • 10
  • 46
  • 77
  • `network.RemoveNetworkDrive("z:")` didn't do it for you? – Robert Harvey Jul 11 '13 at 22:44
  • 2
    Check for the existence of z first, or just delete it first, if dleting a non-existent mapping doesn't throw an error. Course the fact that it exists, could mean the user has their own mapping using that letter, and you wiping it out would irritate them immensely... – Tony Hopkinson Jul 11 '13 at 22:48

2 Answers2

1

Just Use System.Diagnostics.Process.Start("CMD.exe","/c net use * /delete /y");

CD RW
  • 11
  • 1
-1

Just use Process.Start

System.Diagnostics.Process.Start("CMD.exe","/c net use * /delete");

If you insist on a managed code approach (why?) you can do something like:

foreach(var letter in "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
{
   try {
       network.RemoveNetworkDrive(letter + ":");
   } catch {}
}

or better yet (depending on how flexible you are with the required behviour), iterate on this instead:

// assumes using System.IO
var networkDrives = DriveInfo.GetDrives().Where(x => x.DriveType == DriveType.Network))
foreach(var networkDrive in networkDrives)

I'd still opt for just using Process.Start as being far cleaner and more reliable. Once you start down the road of re-implementing 'trivial' functionality you often quickly find out how non-trivial it really is.

Further on why your question itself might need reconsidering - why would you insist on removing all network drive mappings anyway? If you are mapping Z: for example, you know what drive needs to be unmapped. You can have something like this:

public void MapDrive(char driveLetter, string networkPath, string userName, string password)
{
   try { network.RemoveNetworkDrive(driveLetter + ":"); } catch {}
   network.MapNetworkDrive(driveLetter  + ":", @shared_path, Type.Missing, username, password);
}

because surely if you're creating the drive mappings you inherently know which drive letters need to be free as opposed to blowing all network drive mappings away.

nathanchere
  • 8,008
  • 15
  • 65
  • 86
  • 3
    Sure, but surely there's a C# equivalent. Nobody drops to DOS and runs a command line utility when a native alternative is available. – Robert Harvey Jul 11 '13 at 23:29
  • That is as 'native' an alternative as you will get. If you mean you want a managed code alternative, yes it can surely be done but what is the driver for not just using a command line process? – nathanchere Jul 12 '13 at 00:24
  • 1
    CMD.exe doesn't `Process.Start()` another program to perform this function. Just sayin'. – Robert Harvey Jul 12 '13 at 02:21