SelectPhysicalDiskForm.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 SelectPhysicalDiskForm : BaseForm
  10. {
  11. private PhysicalDisk m_selectedDisk;
  12. public SelectPhysicalDiskForm()
  13. {
  14. InitializeComponent();
  15. }
  16. private void SelectPhysicalDiskForm_Load(object sender, EventArgs e)
  17. {
  18. List<PhysicalDisk> physicalDisks = PhysicalDiskHelper.GetPhysicalDisks();
  19. if (Environment.OSVersion.Version.Major >= 6)
  20. {
  21. listPhysicalDisks.Columns.Add("Status", 60);
  22. columnDescription.Width -= 60;
  23. }
  24. foreach (PhysicalDisk physicalDisk in physicalDisks)
  25. {
  26. string title = String.Format("Disk {0}", physicalDisk.PhysicalDiskIndex);
  27. string description = physicalDisk.Description;
  28. string serialNumber = physicalDisk.SerialNumber;
  29. string sizeString = FormattingHelper.GetStandardSizeString(physicalDisk.Size);
  30. ListViewItem item = new ListViewItem(title);
  31. item.SubItems.Add(description);
  32. item.SubItems.Add(serialNumber);
  33. item.SubItems.Add(sizeString);
  34. if (Environment.OSVersion.Version.Major >= 6)
  35. {
  36. var isOnline = physicalDisk.GetOnlineStatus(out string err);
  37. if (isOnline == null)
  38. {
  39. item.SubItems.Add(err);
  40. }
  41. else
  42. {
  43. string status = isOnline.Value ? "Online" : "Offline";
  44. item.SubItems.Add(status);
  45. }
  46. }
  47. item.Tag = physicalDisk;
  48. listPhysicalDisks.Items.Add(item);
  49. }
  50. }
  51. private void btnOK_Click(object sender, EventArgs e)
  52. {
  53. PhysicalDisk selectedDisk;
  54. if (listPhysicalDisks.SelectedItems.Count > 0)
  55. {
  56. selectedDisk = (PhysicalDisk)listPhysicalDisks.SelectedItems[0].Tag;
  57. }
  58. else
  59. {
  60. MessageBox.Show("No disk was selected", "Error");
  61. return;
  62. }
  63. if (!chkReadOnly.Checked)
  64. {
  65. if (Environment.OSVersion.Version.Major >= 6)
  66. {
  67. bool isDiskReadOnly;
  68. bool isOnline = selectedDisk.GetOnlineStatus(out isDiskReadOnly, out _) ?? false;
  69. if (isDiskReadOnly)
  70. {
  71. MessageBox.Show("The selected disk is set to readonly", "Error");
  72. return;
  73. }
  74. if (isOnline)
  75. {
  76. DialogResult result = MessageBox.Show("The selected disk will now be taken offline. OK?", String.Empty, MessageBoxButtons.OKCancel);
  77. if (result == DialogResult.OK)
  78. {
  79. bool success = selectedDisk.SetOnlineStatus(false);
  80. if (!success)
  81. {
  82. MessageBox.Show("Was not able to take the disk offline", "Error");
  83. return;
  84. }
  85. }
  86. else
  87. {
  88. return;
  89. }
  90. }
  91. }
  92. else
  93. {
  94. if (DynamicDisk.IsDynamicDisk(selectedDisk))
  95. {
  96. // The user will probably want to stop the Logical Disk Manager services (vds, dmadmin, dmserver)
  97. // and lock all dynamic disks and dynamic volumes before whatever he's doing.
  98. // Modifications the the LDM database should be applied to all dynamic disks.
  99. DialogResult result = MessageBox.Show("The dynamic disk database will likely be corrupted, Continue?", "Warning", MessageBoxButtons.YesNo);
  100. if (result != DialogResult.Yes)
  101. {
  102. return;
  103. }
  104. }
  105. else
  106. {
  107. // Locking a disk does not prevent Windows from accessing mounted volumes on it. (it does prevent creation of new volumes).
  108. // For basic disks we need to lock the Disk and Volumes, and we should also call UpdateDiskProperties() after releasing the lock.
  109. LockStatus status = LockHelper.LockBasicDiskAndVolumesOrNone(selectedDisk);
  110. if (status == LockStatus.CannotLockDisk)
  111. {
  112. MessageBox.Show("Unable to lock the disk", "Error");
  113. return;
  114. }
  115. else if (status == LockStatus.CannotLockVolume)
  116. {
  117. MessageBox.Show("Unable to lock one of the volumes on the disk", "Error");
  118. return;
  119. }
  120. }
  121. }
  122. }
  123. if (chkReadOnly.Checked)
  124. {
  125. selectedDisk = new PhysicalDisk(selectedDisk.PhysicalDiskIndex, true);
  126. }
  127. m_selectedDisk = selectedDisk;
  128. this.DialogResult = DialogResult.OK;
  129. this.Close();
  130. }
  131. private void btnCancel_Click(object sender, EventArgs e)
  132. {
  133. this.DialogResult = DialogResult.Cancel;
  134. this.Close();
  135. }
  136. public PhysicalDisk SelectedDisk
  137. {
  138. get
  139. {
  140. return m_selectedDisk;
  141. }
  142. }
  143. }
  144. }