123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- using DiskAccessLibrary;
- using DiskAccessLibrary.Mod;
- using ISCSIConsole.Mods;
- using System;
- using System.Windows.Forms;
- namespace ISCSIConsole
- {
- public partial class CreateRAMDiskForm : BaseForm
- {
- public CreateRAMDiskForm()
- {
- InitializeComponent();
- }
- private void btnCancel_Click(object sender, EventArgs e)
- {
- DialogResult = DialogResult.Cancel;
- Close();
- }
- private void BaseOnCheckBox_CheckedChanged(object sender, EventArgs e)
- {
- ImageFileTextBox.Enabled = BaseOnCheckBox.Checked;
- }
- private void ImageFileButton_Click(object sender, EventArgs e)
- {
- if (false == BaseOnCheckBox.Checked) BaseOnCheckBox.Checked = true;
- var result = openDiskImageDialog.ShowDialog();
- if (result == DialogResult.OK)
- {
- ImageFileTextBox.Text = openDiskImageDialog.FileName;
- DiskInfiValueLabel.Text = DiskImage.GetDiskImageInfo(ImageFileTextBox.Text);
- }
- }
- private void btnOK_Click(object sender, EventArgs e)
- {
- BlockDifferencingDiskImage bdd = null;
- if (BaseOnCheckBox.Checked) // check size
- {
- if (string.IsNullOrWhiteSpace(ImageFileTextBox.Text))
- {
- MessageBox.Show("Please choose file location.", "Error");
- return;
- }
- bdd = new BlockDifferencingDiskImage(ImageFileTextBox.Text, true);
- if (false == bdd.IsNoBaseImage)
- {
- MessageBox.Show("Only ** No Base Image Mode ** supported");
- return;
- }
- long diskSize;
- if (MbRadioButton.Checked)
- {
- diskSize = (int)numericDiskSize.Value * Consts.MegaByte;
- }
- else if (GbRadioButton.Checked)
- {
- diskSize = (long)numericDiskSize.Value * Consts.Gigabyte;
- }
- else
- {
- MessageBox.Show("What chosen?");
- return;
- }
- if (diskSize < bdd.Size)
- {
- MessageBox.Show("Ram disk size too small to load file");
- return;
- }
- }
- ILoadableRamDisk loadableRamDisk;
- if (MbRadioButton.Checked)
- {
- var size = (int)numericDiskSize.Value * Consts.MegaByte;
- RAMDisk disk;
- try
- {
- disk = new RAMDisk(size);
- }
- catch (OutOfMemoryException)
- {
- MessageBox.Show("Not enough memory available", "Error");
- return;
- }
- RAMDisk = disk;
- loadableRamDisk = disk;
- }
- else if (GbRadioButton.Checked)
- {
- var size = (int)numericDiskSize.Value;
- var disk = new UnmanagedGigabyteBlockSeparatedRamDisk(size); ;
- try
- {
- disk.Allocate();
- }
- catch (OutOfMemoryException)
- {
- disk.Free();
- MessageBox.Show("Not enough memory available", "Error");
- return;
- }
- RAMDisk = disk;
- loadableRamDisk = disk;
- }
- else
- {
- MessageBox.Show("What chosen?");
- return;
- }
- if (null != bdd) //Load in to ram disk
- {
- bdd.OpenForRead();
- var buffer = new byte[bdd.BlockSize];
- for (var i = 0; i < bdd.NumberOfBlocks; i++)
- {
- if (false == bdd.IsBlockAllocated[i]) continue;
- bdd.ReadBlock(buffer, 0, buffer.Length, i, 0);
- loadableRamDisk.WriteData((long)i * bdd.BlockSize, buffer);
- }
- bdd.CloseForRead();
- }
- DialogResult = DialogResult.OK;
- Close();
- }
- public Disk RAMDisk { get; private set; }
- }
- }
|