private void btnOK_Click(object sender, EventArgs e) { try { this.Cursor = Cursors.WaitCursor; var isFormatVhdX = radFormatVhdX.Checked; var isFormatVhd = !isFormatVhdX; var isTypeFixed = radTypeFixed.Checked; var isTypeDynamic = !isTypeFixed; var filter = isFormatVhdX ? "Virtual disk files (*.vhdx)|*.vhdx|All files (*.*)|*.*" : "Virtual disk files (*.vhd)|*.vhd|All files (*.*)|*.*"; using (var frm = new SaveFileDialog() { AddExtension = true, AutoUpgradeEnabled = true, Filter = filter, FilterIndex = 0, OverwritePrompt = true, Title = "New disk", ValidateNames = true }) { if (frm.ShowDialog(this) == DialogResult.OK) { this.FileName = frm.FileName; } else { return; } } DriveInfo drive = null; try { drive = new DriveInfo(this.FileName); if (drive.DriveFormat.Equals("NTFS", StringComparison.OrdinalIgnoreCase) == false) { if ((Environment.OSVersion.Version.Major * 1000000 + Environment.OSVersion.Version.Minor) < 6000002) //Windows 8 { if (isFormatVhd && isTypeFixed) { if (Medo.MessageBox.ShowWarning(this, "Due to operating system limitations, virtual disk created on this drive will not be attachable.\nIn order to attach virtual disk it will need to be on NTFS-formatted drive.\n\nDo you wish to continue?", MessageBoxButtons.YesNo) == DialogResult.No) { return; } } else { if (Medo.MessageBox.ShowWarning(this, "Due to operating system limitations, virtual disk cannot be created.\nIn order to create and attach virtual disk it will need to be on NTFS-formatted drive.\n\nDo you wish to continue anyway?", MessageBoxButtons.YesNo, MessageBoxDefaultButton.Button2) == DialogResult.No) { return; } } } } } catch (ArgumentException) { } try { File.Delete(this.FileName); } catch (IOException ex) { this.Cursor = Cursors.Default; Medo.MessageBox.ShowError(this, "File cannot be deleted.\n\n" + ex.Message); return; } catch (UnauthorizedAccessException ex) { this.Cursor = Cursors.Default; Medo.MessageBox.ShowError(this, "File cannot be deleted.\n\n" + ex.Message); return; } var diskSize = GetSizeInBytes(); if (drive != null) { if (drive.AvailableFreeSpace < diskSize) //yes, no overhead is calculated in { if (isTypeFixed) { if (Medo.MessageBox.ShowWarning(this, string.Format("There is not enough free space available!\nVirtual disk will require {0} while drive has only {1} free.\n\nDo you wish to continue?", BinaryPrefixExtensions.ToBinaryPrefixString(diskSize, "B", "0"), BinaryPrefixExtensions.ToBinaryPrefixString(drive.AvailableFreeSpace, "B", "0")), MessageBoxButtons.YesNo) == DialogResult.No) { return; } } else { if (Medo.MessageBox.ShowInformation(this, string.Format("This disk can expand to size larger than free space available at the moment.\nVirtual disk will require {0} while drive has only {1} free.\n\nDo you wish to continue?", BinaryPrefixExtensions.ToBinaryPrefixString(diskSize, "B", "0"), BinaryPrefixExtensions.ToBinaryPrefixString(drive.AvailableFreeSpace, "B", "0")), MessageBoxButtons.YesNo) == DialogResult.No) { return; } } } if (drive.DriveFormat.Equals("FAT32", StringComparison.OrdinalIgnoreCase) && (diskSize > int.MaxValue * 0.9)) //just give warning if getting close to 2GB limit { if (Medo.MessageBox.ShowWarning(this, "Due to operating system limitations it will not be possible to create virtual disk larger than 2 GB.\n\nDo you wish to continue?", MessageBoxButtons.YesNo) == DialogResult.No) { return; } } } try { try { if (isTypeFixed) { using (var frm = new CreateFixedDiskForm(this.FileName, diskSize, isFormatVhdX)) { if (frm.ShowDialog(this) == DialogResult.Cancel) { return; } } } else //Dynamic { using (var vhd = new Medo.IO.VirtualDisk(this.FileName)) { if (isFormatVhdX) { vhd.Create(diskSize, Medo.IO.VirtualDiskCreateOptions.None, 0, 0, Medo.IO.VirtualDiskType.Vhdx); } else { vhd.Create(diskSize, Medo.IO.VirtualDiskCreateOptions.None, 0, 0, Medo.IO.VirtualDiskType.Vhd); } } } } catch (Exception ex) { throw new InvalidOperationException(ex.Message, ex); } } catch (InvalidOperationException ex) { this.Cursor = Cursors.Default; Medo.MessageBox.ShowError(this, "Virtual disk cannot be created.\n\n" + ex.Message); return; } this.DialogResult = DialogResult.OK; } finally { this.Cursor = Cursors.Default; } }
private void btnOK_Click(object sender, EventArgs e) { try { this.Cursor = Cursors.WaitCursor; var isFormatVhdX = radFormatVhdX.Checked; var isFormatVhd = !isFormatVhdX; var isTypeFixed = radTypeFixed.Checked; var isTypeDynamic = !isTypeFixed; var filter = isFormatVhdX ? "Virtual disk files (*.vhdx)|*.vhdx|All files (*.*)|*.*" : "Virtual disk files (*.vhd)|*.vhd|All files (*.*)|*.*"; using (var frm = new SaveFileDialog() { AddExtension = true, AutoUpgradeEnabled = true, Filter = filter, FilterIndex = 0, OverwritePrompt = true, Title = "New disk", ValidateNames = true }) { if (frm.ShowDialog(this) == DialogResult.OK) { this.FileName = frm.FileName; } else { return; } } DriveInfo drive = null; try { drive = new DriveInfo(this.FileName); if (drive.DriveFormat.Equals("NTFS", StringComparison.OrdinalIgnoreCase) == false) { if ((Environment.OSVersion.Version.Major * 1000000 + Environment.OSVersion.Version.Minor) < 6000002) { //Windows 8 if (isFormatVhd && isTypeFixed) { if (Medo.MessageBox.ShowWarning(this, "Due to operating system limitations, virtual disk created on this drive will not be attachable.\nIn order to attach virtual disk it will need to be on NTFS-formatted drive.\n\nDo you wish to continue?", MessageBoxButtons.YesNo) == DialogResult.No) { return; } } else { if (Medo.MessageBox.ShowWarning(this, "Due to operating system limitations, virtual disk cannot be created.\nIn order to create and attach virtual disk it will need to be on NTFS-formatted drive.\n\nDo you wish to continue anyway?", MessageBoxButtons.YesNo, MessageBoxDefaultButton.Button2) == DialogResult.No) { return; } } } } } catch (ArgumentException) { } try { File.Delete(this.FileName); } catch (IOException ex) { this.Cursor = Cursors.Default; Medo.MessageBox.ShowError(this, "File cannot be deleted.\n\n" + ex.Message); return; } catch (UnauthorizedAccessException ex) { this.Cursor = Cursors.Default; Medo.MessageBox.ShowError(this, "File cannot be deleted.\n\n" + ex.Message); return; } var diskSize = GetSizeInBytes(); if (drive != null) { if (drive.AvailableFreeSpace < diskSize) { //yes, no overhead is calculated in if (isTypeFixed) { if (Medo.MessageBox.ShowWarning(this, string.Format("There is not enough free space available!\nVirtual disk will require {0} while drive has only {1} free.\n\nDo you wish to continue?", BinaryPrefixExtensions.ToBinaryPrefixString(diskSize, "B", "0"), BinaryPrefixExtensions.ToBinaryPrefixString(drive.AvailableFreeSpace, "B", "0")), MessageBoxButtons.YesNo) == DialogResult.No) { return; } } else { if (Medo.MessageBox.ShowInformation(this, string.Format("This disk can expand to size larger than free space available at the moment.\nVirtual disk will require {0} while drive has only {1} free.\n\nDo you wish to continue?", BinaryPrefixExtensions.ToBinaryPrefixString(diskSize, "B", "0"), BinaryPrefixExtensions.ToBinaryPrefixString(drive.AvailableFreeSpace, "B", "0")), MessageBoxButtons.YesNo) == DialogResult.No) { return; } } } if (drive.DriveFormat.Equals("FAT32", StringComparison.OrdinalIgnoreCase) && (diskSize > int.MaxValue * 0.9)) { //just give warning if getting close to 2GB limit if (Medo.MessageBox.ShowWarning(this, "Due to operating system limitations it will not be possible to create virtual disk larger than 2 GB.\n\nDo you wish to continue?", MessageBoxButtons.YesNo) == DialogResult.No) { return; } } } try { try { if (isTypeFixed) { using (var frm = new CreateFixedDiskForm(this.FileName, diskSize, isFormatVhdX)) { if (frm.ShowDialog(this) == DialogResult.Cancel) { return; } } } else { //Dynamic using (var vhd = new Medo.IO.VirtualDisk(this.FileName)) { if (isFormatVhdX) { vhd.Create(diskSize, Medo.IO.VirtualDiskCreateOptions.None, 0, 0, Medo.IO.VirtualDiskType.Vhdx); } else { vhd.Create(diskSize, Medo.IO.VirtualDiskCreateOptions.None, 0, 0, Medo.IO.VirtualDiskType.Vhd); } } } } catch (Exception ex) { throw new InvalidOperationException(ex.Message, ex); } } catch (InvalidOperationException ex) { this.Cursor = Cursors.Default; Medo.MessageBox.ShowError(this, "Virtual disk cannot be created.\n\n" + ex.Message); return; } this.DialogResult = DialogResult.OK; } finally { this.Cursor = Cursors.Default; } }