private void MountNetworkDrive(Models.NetworkCredential cred) { string message = null; try { DriveInfo drive = GetDriveInfo(cred.MountPoint); if (drive != null) { string userName = null; string remotePath = MappedDriveResolver.ResolveToRootUNC(cred.MountPoint); string credentialUsed = MappedDriveResolver.GetCredentialUsedToMapNetworkDrive(cred.MountPoint); message = string.Format("{0} is already mounted to {1} by {2}", cred.MountPoint, remotePath, credentialUsed); logger.Info(message); // Was this mounted using the same credential? // We assume the mount point is OK if we cannot retrieve the username that originally mounted it. if (userName != null && (userName.Equals(cred.Login, StringComparison.InvariantCulture) || userName.Equals(MappedDriveResolver.UNKNOWN_CREDENTIAL, StringComparison.InvariantCulture))) { return; // We're OK then. } NetworkDriveMapper.UnmountNetworkLocation(cred.MountPoint); logger.Info("Umounted {0}", cred.MountPoint); } NetworkDriveMapper.MountNetworkLocation(cred.MountPoint, cred.Path, cred.Login, cred.Password, false); message = string.Format("Successfully mounted {0} to {1} as {2}", cred.MountPoint, cred.Path, cred.Login); logger.Info(message); } catch (Win32Exception ex) { string reason = ex.Message; switch (ex.NativeErrorCode) { case NetworkDriveMapper.ERROR_ALREADY_ASSIGNED: { //string userName = null; string remotePath = MappedDriveResolver.ResolveToRootUNC(cred.MountPoint); string credentialUsed = MappedDriveResolver.GetCredentialUsedToMapNetworkDrive(cred.MountPoint); reason = string.Format("It's already mounted to {0} by {1}", remotePath, credentialUsed); break; } } message = string.Format("Failed to mount {0} to {1} as {2} - {3}", cred.MountPoint, cred.Path, cred.Login, reason); throw new FailedToMountNetworkDrive(message); } }