public DiskReader(DiskInfo diskInfo) { _diskInfo = diskInfo; _volumeHandles = new Stack <SafeFileHandle>(_diskInfo.Drives?.Length ?? 0); OpenVolumes(); _deviceHandle = DiskUtils.GetDeviceHandle(diskInfo.DiskNumber); _stream = new FileStream(_deviceHandle, FileAccess.Read, CommonSizes._128K, false); _disposed = false; _closed = false; _totalRead = 0; }
public static DiskInfo[] GetDisks(DriveInfo[] drives, bool onlyRemovable = true, bool onlyReady = true) { var diskInformation = new Dictionary <ushort, DiskInfo>(); foreach (var drive in drives) { if (onlyReady && !drive.IsReady) { continue; } if (onlyRemovable && drive.DriveType != DriveType.Removable) { continue; } var diskNumbers = DiskUtils.GetPhysicalDiskNumbersFromDrive(drive.RootDirectory.Name); foreach (var diskNumber in diskNumbers) { DiskInfo diskInfo; if (!diskInformation.TryGetValue(diskNumber, out diskInfo)) { diskInfo = new DiskInfo() { DiskNumber = diskNumber, Drives = new DriveInfo[0] }; diskInformation.Add(diskNumber, diskInfo); } diskInfo.Drives = Utils.Add <DriveInfo>(diskInfo.Drives, drive) .OrderBy(_ => _.Name) .ToArray(); } } foreach (var kvp in diskInformation) { kvp.Value.Size = DiskUtils.GetPhysicalDiskSize(kvp.Key); kvp.Value.SectorSize = DiskUtils.GetPhysicalDiskSectorSize(kvp.Key); kvp.Value.SectorCount = DiskUtils.GetPhysicalDiskSectorCount(kvp.Key); } var results = diskInformation.Values.OrderBy(_ => _.DiskNumber) .ToArray(); return(results); }
private void OnDeviceAddRemove(object sender, DriveDetectorEventArgs e) { _disks = DiskInfo.GetDisks(); ddlDevice.Items.Clear(); foreach (var disk in _disks) { ddlDevice.Items.Add(disk); if (_selectedDisk != null && _selectedDisk.DiskNumber == disk.DiskNumber) { ddlDevice.SelectedItem = disk; } } }
public MainForm() { InitializeComponent(); _driveDetector = new DriveDetector(); _disks = DiskInfo.GetDisks(); _cts = null; _selectedDisk = null; _imageFilename = string.Empty; _hashFilename = string.Empty; _started = 0; _total = 0; _totalWrite = 0; _writeRate = 0; Disposed += OnDisposed; _driveDetector.DeviceArrived += OnDeviceAddRemove; _driveDetector.DeviceRemoved += OnDeviceAddRemove; }
private async void btnWrite_Click(object sender, EventArgs e) { if (MessageBox.Show("This operation will overwrite any existing data on the selected physical disk. Are you sure you want to continue?", "Confirm Write", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) { return; } timer.Stop(); processTimer.Start(); gbxImage.Enabled = false; gbxDisk.Enabled = false; gbxHash.Enabled = false; gbxProgress.Enabled = true; btnWrite.Enabled = false; btnCancel.Enabled = true; _cts = new CancellationTokenSource(); _selectedDisk = (DiskInfo)ddlDevice.SelectedItem; _imageFilename = txtFilename.Text; _hashFilename = txtHash.Text; Interlocked.Exchange(ref _started, DateTime.UtcNow.Ticks); try { if (!string.IsNullOrWhiteSpace(txtHash.Text) && File.Exists(txtHash.Text)) { if (!await ValidateHashAsync()) { return; } await Task.Delay(1000); // Lets the progress get to 100% } if (!await WriteImageFileAsync()) { return; } await Task.Delay(1000); // Lets the progress get to 100% } catch (Exception ex) { MessageBox.Show(ex.Message, "Disk Image Writer Error", MessageBoxButtons.OK, MessageBoxIcon.Error); #if DEBUG throw; // When debugging re-throw for the debugger #endif } finally { _cts.Dispose(() => _cts = null); gbxImage.Enabled = true; gbxDisk.Enabled = true; gbxHash.Enabled = true; gbxProgress.Enabled = false; lblTimeTakenValue.Text = string.Empty; lblPercentCompleteValue.Text = string.Empty; lblSpeedValue.Text = string.Empty; lblStatus.Text = string.Empty; progressBar.Value = 0; processTimer.Stop(); timer.Start(); } }
private async void btnCreate_Click(object sender, EventArgs e) { timer.Stop(); processTimer.Start(); gbxDisk.Enabled = false; gbxImage.Enabled = false; gbxOptions.Enabled = false; gbxProgress.Enabled = true; btnCreate.Enabled = false; btnCancel.Enabled = true; _cts = new CancellationTokenSource(); _selectedDisk = (DiskInfo)ddlDevice.SelectedItem; _imageFilename = txtFilename.Text; _hashFilename = string.Empty; _hash = ddlHash.SelectedItem.ToString(); _compression = ddlCompression.SelectedItem.ToString(); Interlocked.Exchange(ref _started, DateTime.UtcNow.Ticks); try { await CreateImageFileAsync(); await CreateHashFileAsync(); } catch (Exception ex) { if (File.Exists(_imageFilename)) { File.Delete(_imageFilename); } if (!string.IsNullOrWhiteSpace(_hashFilename) && File.Exists(_hashFilename)) { File.Delete(_hashFilename); } MessageBox.Show(ex.Message, "Disk Image Reader Error", MessageBoxButtons.OK, MessageBoxIcon.Error); #if DEBUG throw; // When debugging re-throw for the debugger #endif } finally { _cts.Dispose(() => _cts = null); gbxDisk.Enabled = true; gbxImage.Enabled = true; gbxOptions.Enabled = true; gbxProgress.Enabled = false; lblTimeTakenValue.Text = string.Empty; lblPercentCompleteValue.Text = string.Empty; lblSpeedValue.Text = string.Empty; lblStatus.Text = string.Empty; progressBar.Value = 0; processTimer.Stop(); timer.Start(); } }