SelectVolumeForm.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Text;
  6. using System.Windows.Forms;
  7. using DiskAccessLibrary;
  8. using DiskAccessLibrary.LogicalDiskManager;
  9. using Utilities;
  10. namespace ISCSIConsole
  11. {
  12. public partial class SelectVolumeForm : Form
  13. {
  14. private Volume m_selectedVolume;
  15. private bool m_isReadOnly;
  16. public SelectVolumeForm()
  17. {
  18. InitializeComponent();
  19. }
  20. private void SelectPhysicalDiskForm_Load(object sender, EventArgs e)
  21. {
  22. List<Volume> volumes = WindowsVolumeHelper.GetVolumes();
  23. for(int index = 0; index < volumes.Count; index++)
  24. {
  25. Volume volume = volumes[index];
  26. string title = String.Format("Volume {0}", index);
  27. string type = VolumeHelper.GetVolumeTypeString(volume);
  28. string status = VolumeHelper.GetVolumeStatusString(volume);
  29. ulong volumeID = 0;
  30. string name = String.Empty;
  31. if (volume is DynamicVolume)
  32. {
  33. volumeID = ((DynamicVolume)volume).VolumeID;
  34. name = ((DynamicVolume)volume).Name;
  35. }
  36. else if (volume is GPTPartition)
  37. {
  38. name = ((GPTPartition)volume).PartitionName;
  39. }
  40. ListViewItem item = new ListViewItem(title);
  41. item.SubItems.Add(name);
  42. item.SubItems.Add(type);
  43. item.SubItems.Add(status);
  44. item.SubItems.Add(FormattingHelper.GetStandardSizeString(volume.Size));
  45. item.Tag = volume;
  46. listPhysicalDisks.Items.Add(item);
  47. }
  48. }
  49. private void btnOK_Click(object sender, EventArgs e)
  50. {
  51. if (listPhysicalDisks.SelectedItems.Count > 0)
  52. {
  53. Volume selectedVolume = (Volume)listPhysicalDisks.SelectedItems[0].Tag;
  54. if (!chkReadOnly.Checked)
  55. {
  56. Guid? volumeGuid = WindowsVolumeHelper.GetWindowsVolumeGuid(selectedVolume);
  57. if (Environment.OSVersion.Version.Major >= 6)
  58. {
  59. // Windows Vista / 7 enforce various limitations on direct write operations to volumes and disks.
  60. // We either have to take the disk(s) offline or use the OS volume handle for write operations.
  61. if (!volumeGuid.HasValue)
  62. {
  63. MessageBox.Show("The selected volume is not recognized by your operating system");
  64. return;
  65. }
  66. selectedVolume = new OperatingSystemVolume(volumeGuid.Value, selectedVolume.BytesPerSector, selectedVolume.Size, chkReadOnly.Checked);
  67. }
  68. bool isLocked = false;
  69. if (volumeGuid.HasValue)
  70. {
  71. isLocked = WindowsVolumeManager.ExclusiveLock(volumeGuid.Value);
  72. }
  73. if (!isLocked)
  74. {
  75. MessageBox.Show("Unable to lock the volume", "Error");
  76. return;
  77. }
  78. }
  79. m_selectedVolume = selectedVolume;
  80. m_isReadOnly = chkReadOnly.Checked;
  81. this.DialogResult = DialogResult.OK;
  82. this.Close();
  83. }
  84. else
  85. {
  86. MessageBox.Show("No volume was selected", "Error");
  87. return;
  88. }
  89. }
  90. private IList<PhysicalDisk> GetVolumeDisks(Volume volume)
  91. {
  92. SortedList<int, PhysicalDisk> disks = new SortedList<int,PhysicalDisk>();
  93. if (volume is DynamicVolume)
  94. {
  95. foreach (DiskExtent extent in ((DynamicVolume)volume).Extents)
  96. {
  97. if (extent.Disk is PhysicalDisk)
  98. {
  99. PhysicalDisk disk = (PhysicalDisk)extent.Disk;
  100. if (!disks.ContainsKey(disk.PhysicalDiskIndex))
  101. {
  102. disks.Add(disk.PhysicalDiskIndex, disk);
  103. }
  104. }
  105. }
  106. }
  107. else if (volume is Partition)
  108. {
  109. Partition partition = (Partition)volume;
  110. if (partition.Disk is PhysicalDisk)
  111. {
  112. PhysicalDisk disk = (PhysicalDisk)partition.Disk;
  113. disks.Add(disk.PhysicalDiskIndex, (PhysicalDisk)disk);
  114. }
  115. }
  116. return disks.Values;
  117. }
  118. private void btnCancel_Click(object sender, EventArgs e)
  119. {
  120. this.DialogResult = DialogResult.Cancel;
  121. this.Close();
  122. }
  123. public Volume SelectedVolume
  124. {
  125. get
  126. {
  127. return m_selectedVolume;
  128. }
  129. }
  130. public bool IsReadOnly
  131. {
  132. get
  133. {
  134. return m_isReadOnly;
  135. }
  136. }
  137. private void listPhysicalDisks_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
  138. {
  139. e.NewWidth = ((ListView)sender).Columns[e.ColumnIndex].Width;
  140. e.Cancel = true;
  141. }
  142. }
  143. }