Exemplo n.º 1
0
        private void btnOK_Click(object sender, EventArgs e)
        {
            var driveLetter = string.IsNullOrEmpty(cmbDriveLetter.Text) ? "" : cmbDriveLetter.Text + "\\";

            using (var frm = new ServiceWaitForm("Changing drive letter",
                                                 delegate() {
                var res = PipeClient.ChangeDriveLetter(this.Volume.VolumeName, driveLetter);
                if (res.IsError)
                {
                    throw new InvalidOperationException(res.Message);
                }
            })) {
                frm.ShowDialog(this);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Change the drive letter of an attached VHD.
        /// <summary>
        public int ChangeDriveLetter(string[] args)
        {
            // Init and parameter checks

            if (args.Length != 2)
            {
                string err = "Changing drive letter failed. Wrong number of arguments.\n";
                err += "Usage: VhdAttach -ChangeLetter VHDFilename Driveletter:";
                showError(err);
                return(1);
            }

            string fileName = args[0];

            if (!File.Exists(fileName))
            {
                string err = "Changing drive letter failed. File not found: " + fileName + "\n";
                err += "Usage: VhdAttach -ChangeLetter VHDFilename Driveletter:";
                showError(err);
                return(1);
            }

            string driveLetterRaw = args[1];

            if (driveLetterRaw.Length > 3 || (driveLetterRaw.Length == 3 && driveLetterRaw.Substring(1, 2) != ":\\") || (driveLetterRaw.Length == 2 && driveLetterRaw.Substring(1, 1) != ":"))
            {
                string err = "Changing drive letter failed. Drive Letter parameter seems to be invalid.\n";
                err += "Usage: VhdAttach -ChangeLetter VHDFilename Driveletter:";
                showError(err);
                return(1);
            }

            string allowedLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
            string driveLetter1   = args[1].Substring(0, 1);
            string driveLetter2   = driveLetter1 + ":";

            if (allowedLetters.IndexOf(driveLetter1) == -1)
            {
                string err = "Changing drive letter failed. Drive Letter parameter seems to be invalid.\n";
                err += "Usage: VhdAttach -ChangeLetter VHDFilename Driveletter:";
                showError(err);
                return(1);
            }

            // Test-Code, please ignore
            // PipeClient.Attach(fileName, false, false);
            // PipeClient.Detach(fileName);

            // The actual drive letter operation begins here

            string attachedDevice = null;

            try {
                using (var document = new Medo.IO.VirtualDisk(fileName)) {
                    document.Open(Medo.IO.VirtualDiskAccessMask.GetInfo);
                    attachedDevice = document.GetAttachedPath();
                }
            } catch {
                string err = "Changing Drive letter failed. Could not open device.\n";
                err += "Possible reasons include:\n";
                err += "The specified file might not be attached or maybe isn't an VHD file.";
                showError(err);
                return(1);
            }

            if (attachedDevice != null)
            {
                var volumes          = new List <Volume>(Volume.GetVolumesOnPhysicalDrive(attachedDevice));
                var availableVolumes = new List <Volume>();
                if (volumes != null && volumes.Count > 0)
                {
                    string       oldLetter2 = volumes[0].DriveLetter2;
                    PipeResponse res        = PipeClient.ChangeDriveLetter(volumes[0].VolumeName, driveLetter2);
                    if (res.IsError == true)
                    {
                        PipeClient.ChangeDriveLetter(volumes[0].VolumeName, oldLetter2);
                        string err = "Changing drive letter failed. Drive letter possibly in use.";
                        showError(err);
                        return(1);
                    }
                }
            }

            return(0);
        }