CreateRAMDiskForm.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using DiskAccessLibrary;
  2. using DiskAccessLibrary.Mod;
  3. using ISCSIConsole.Mods;
  4. using System;
  5. using System.Windows.Forms;
  6. namespace ISCSIConsole
  7. {
  8. public partial class CreateRAMDiskForm : BaseForm
  9. {
  10. public CreateRAMDiskForm()
  11. {
  12. InitializeComponent();
  13. }
  14. private void btnCancel_Click(object sender, EventArgs e)
  15. {
  16. DialogResult = DialogResult.Cancel;
  17. Close();
  18. }
  19. private void BaseOnCheckBox_CheckedChanged(object sender, EventArgs e)
  20. {
  21. ImageFileTextBox.Enabled = BaseOnCheckBox.Checked;
  22. }
  23. private void ImageFileButton_Click(object sender, EventArgs e)
  24. {
  25. if (false == BaseOnCheckBox.Checked) BaseOnCheckBox.Checked = true;
  26. var result = openDiskImageDialog.ShowDialog();
  27. if (result == DialogResult.OK)
  28. {
  29. ImageFileTextBox.Text = openDiskImageDialog.FileName;
  30. DiskInfiValueLabel.Text = DiskImage.GetDiskImageInfo(ImageFileTextBox.Text);
  31. }
  32. }
  33. private void btnOK_Click(object sender, EventArgs e)
  34. {
  35. BlockDifferencingDiskImage bdd = null;
  36. if (BaseOnCheckBox.Checked) // check size
  37. {
  38. if (string.IsNullOrWhiteSpace(ImageFileTextBox.Text))
  39. {
  40. MessageBox.Show("Please choose file location.", "Error");
  41. return;
  42. }
  43. bdd = new BlockDifferencingDiskImage(ImageFileTextBox.Text, true);
  44. if (false == bdd.IsNoBaseImage)
  45. {
  46. MessageBox.Show("Only ** No Base Image Mode ** supported");
  47. return;
  48. }
  49. long diskSize;
  50. if (MbRadioButton.Checked)
  51. {
  52. diskSize = (int)numericDiskSize.Value * Consts.MegaByte;
  53. }
  54. else if (GbRadioButton.Checked)
  55. {
  56. diskSize = (long)numericDiskSize.Value * Consts.Gigabyte;
  57. }
  58. else
  59. {
  60. MessageBox.Show("What chosen?");
  61. return;
  62. }
  63. if (diskSize < bdd.Size)
  64. {
  65. MessageBox.Show("Ram disk size too small to load file");
  66. return;
  67. }
  68. }
  69. ILoadableRamDisk loadableRamDisk;
  70. if (MbRadioButton.Checked)
  71. {
  72. var size = (int)numericDiskSize.Value * Consts.MegaByte;
  73. RAMDisk disk;
  74. try
  75. {
  76. disk = new RAMDisk(size);
  77. }
  78. catch (OutOfMemoryException)
  79. {
  80. MessageBox.Show("Not enough memory available", "Error");
  81. return;
  82. }
  83. RAMDisk = disk;
  84. loadableRamDisk = disk;
  85. }
  86. else if (GbRadioButton.Checked)
  87. {
  88. var size = (int)numericDiskSize.Value;
  89. var disk = new UnmanagedGigabyteBlockSeparatedRamDisk(size); ;
  90. try
  91. {
  92. disk.Allocate();
  93. }
  94. catch (OutOfMemoryException)
  95. {
  96. disk.Free();
  97. MessageBox.Show("Not enough memory available", "Error");
  98. return;
  99. }
  100. RAMDisk = disk;
  101. loadableRamDisk = disk;
  102. }
  103. else
  104. {
  105. MessageBox.Show("What chosen?");
  106. return;
  107. }
  108. if (null != bdd) //Load in to ram disk
  109. {
  110. var buffer = new byte[bdd.BlockSize];
  111. for (var i = 0; i < bdd.NumberOfBlocks; i++)
  112. {
  113. if (false == bdd.IsBlockAllocated[i]) continue;
  114. bdd.ReadBlock(buffer, 0, buffer.Length, i, 0);
  115. loadableRamDisk.WriteData((long)i * bdd.BlockSize, buffer);
  116. }
  117. bdd.ReleaseLock();
  118. }
  119. DialogResult = DialogResult.OK;
  120. Close();
  121. }
  122. public Disk RAMDisk { get; private set; }
  123. }
  124. }