SelectVolumeForm.cs 6.0 KB

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