SelectPhysicalDiskForm.cs 6.1 KB

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