SelectPhysicalDiskForm.cs 6.1 KB

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