Esempio n. 1
0
        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);
            }
        }
Esempio n. 2
0
        private IEnumerator <DriveItem> FetchDriveList()
        {
            yield return(new DriveItem {
                Text = "Please, select a drive"
            });

            DriveInfo[] allDrivesInUse = DriveInfo.GetDrives();
            foreach (char drive in AllDrives)
            {
                string localDriveFullName = drive.ToString() + Path.VolumeSeparatorChar + Path.DirectorySeparatorChar;                 // For example, @"C:\".

                DriveInfo driveInUse = Array.Find(allDrivesInUse, x => x.RootDirectory.FullName == localDriveFullName);

                if (driveInUse != null)
                {
                    DriveItem item           = null;
                    string    localDriveName = FileManager.GetDriveLetter(driveInUse.RootDirectory.FullName);                  // For example: @"C:" (without trailing slash)
                    string    driveType      = driveInUse.DriveType.ToString();

                    switch (driveInUse.DriveType)
                    {
                    default:
                        item = new DriveItem
                        {
                            Text             = string.Format("{0} ({1})", localDriveName, driveType),
                            LocalDrive       = localDriveName,
                            IsDriveAvailable = false,
                        };
                        break;

                    case DriveType.Network:
                        if (ExcludeNetworkDrives)
                        {
                            break;
                        }
                        string mappedPath = MappedDriveResolver.ResolveToRootUNC(driveInUse.RootDirectory.FullName);
                        item = new DriveItem
                        {
                            Text             = string.Format("{0} ({1})", localDriveName, mappedPath),
                            LocalDrive       = localDriveName,
                            MappedPath       = mappedPath,
                            IsDriveAvailable = false,
                        };
                        break;

                    case DriveType.Fixed:
                        if (ExcludeFixedDrives)
                        {
                            break;
                        }
                        item = new DriveItem
                        {
                            Text             = string.Format("{0} ({1})", localDriveName, driveType),
                            LocalDrive       = localDriveName,
                            IsDriveAvailable = false,
                        };
                        break;

                    case DriveType.Removable:
                        if (ExcludeRemovableDrives)
                        {
                            break;
                        }
                        item = new DriveItem
                        {
                            Text             = string.Format("{0} ({1})", localDriveName, driveType),
                            LocalDrive       = localDriveName,
                            IsDriveAvailable = false,
                        };
                        break;

                    case DriveType.CDRom:
                        if (ExcludeCDRomDrives)
                        {
                            break;
                        }
                        item = new DriveItem
                        {
                            Text             = string.Format("{0} ({1})", localDriveName, driveType),
                            LocalDrive       = localDriveName,
                            IsDriveAvailable = false,
                        };
                        break;
                    }

                    if (item != null)
                    {
                        yield return(item);
                    }
                }
                else
                {
                    string localDriveName = FileManager.GetDriveLetter(localDriveFullName);                     // For example: @"C:" (without trailing slash)
                    yield return(new DriveItem
                    {
                        Text = localDriveName,
                        LocalDrive = localDriveName,
                        IsDriveAvailable = true,
                    });
                }
            }
        }
Esempio n. 3
0
        // get all drives
        void RenderDrives(string selectDrive)
        {
            List <string> drives = new List <string>();

            for (int i = 67; i < 91; i++)
            {
                drives.Add((char)i + ":");
            }
            int ndx         = 0;
            int selectIndex = 0;

            foreach (string drive in drives)
            {
                if (drive.StartsWith(selectDrive))
                {
                    selectIndex = ndx;
                }
                // is it a permanently mapped network drive
                string remapAtLogon = "";
                string keyName      = @"HKEY_CURRENT_USER\Network\" + drive.Substring(0, 1);
                string valueName    = "RemotePath";
                string value        = (string)Registry.GetValue(keyName, valueName, null);
                if (value != null)
                {
                    remapAtLogon = String.Format("<remap@logon to '{0}'>", value);
                }
                // skip non pingable drives, otherwise ResolveToUNC(..) will hang
                DriveInfo di = new DriveInfo(drive);
                if (di.DriveType == DriveType.Network)
                {
                    // network drive reachable?
                    if (!GrzTools.Network.PingNetDriveOk(drive /*.Substring(0, 2)*/))
                    {
                        this.comboBoxDrive.Items.Add(drive + " assigned network path is not accessible " + remapAtLogon);
                        ndx++;
                        continue;
                    }
                }
                // resolve network drives
                string mapping = MappedDriveResolver.ResolveToUNC(drive);
                if (mapping == drive + "\\")
                {
                    mapping = "assigned local drive";
                }
                else
                {
                    if (mapping == drive)
                    {
                        mapping = "";
                    }
                    else
                    {
                        this.comboBoxNetworkFolder.Items.Add(mapping);
                    }
                }
                // add info to combo
                this.comboBoxDrive.Items.Add(drive + " " + mapping + " " + remapAtLogon);
                ndx++;
            }

            this.comboBoxDrive.SelectedIndex = selectIndex;
        }