private async void btnWipe_Click(object sender, EventArgs e) { DiskDrive dd = (DiskDrive)lstDiskDrive.SelectedItem; if (dd == null) { MessageBox.Show("Choose valid SD drive first.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (MessageBox.Show("All data on SD card will be erased and partitions deleted. Are you sure?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.No) { return; } Operation = "Wiping"; if (!TryLockVolumes(dd)) { return; } SetButtons(false); using (Stream source = new ConstantStream(0xff), dest = dd.CreateStream(FileAccess.Write)) { var mbr = Properties.Resources.MBR; dest.Write(mbr, 0, mbr.Length); progress.Value = 0; progress.Maximum = (int)(((long)dd.GetDriveSize() - mbr.Length) / (1024 * 1024)) + 1; cts = new CancellationTokenSource(); try { await CopyStreamAsync(source, dest, (long)dd.GetDriveSize() - mbr.Length, cts.Token, null); } catch (Exception ex) { if (!(ex is OperationCanceledException)) { MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK); } cts.Cancel(); } } dd.DismountVolumes(); dd.UnlockVolumes(); dd.MountVolumes(); ResetProgress(); FillDriveList(); if (!cts.IsCancellationRequested) { MessageBox.Show("Erasing complete.", "Complete", MessageBoxButtons.OK, MessageBoxIcon.Information); } SetButtons(true); cts = null; }
private async void btnRead_Click(object sender, EventArgs e) { DiskDrive dd = (DiskDrive)lstDiskDrive.SelectedItem; if (!CheckFilename(txtFilename.Text, dd, true)) { txtFilename.Focus(); return; } var filename = Path.GetFullPath(txtFilename.Text); Operation = "Reading"; if (!TryLockVolumes(dd)) { return; } SetButtons(false); using (Stream dest = new FileStream(txtFilename.Text, FileMode.Create, FileAccess.Write, FileShare.None), source = dd.CreateStream(FileAccess.Read)) { progress.Maximum = (int)(dd.GetDriveSize() / (1024 * 1024)) + 1; progress.Value = 0; cts = new CancellationTokenSource(); try { await CopyStreamAsync(source, dest, (long)dd.GetDriveSize(), cts.Token, null); } catch (Exception ex) { if (!(ex is OperationCanceledException)) { MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK); } cts.Cancel(); } } dd.UnlockVolumes(); if (!cts.IsCancellationRequested) { MessageBox.Show("Reading complete.", "Complete", MessageBoxButtons.OK, MessageBoxIcon.Information); } ResetProgress(); SetButtons(true); cts = null; }
private async void btnFormat_Click(object sender, EventArgs e) { DiskDrive dd = (DiskDrive)lstDiskDrive.SelectedItem; if (dd == null) { MessageBox.Show("Choose valid SD drive first.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (MessageBox.Show("All data on SD card will be lost during format. Are you sure?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.No) { return; } SetButtons(false); //if (dd.Volumes.Count() == 0) { var handle = dd.CreateHandle(FileAccess.ReadWrite); var x = IOWrapper.DiskGetDriveLayoutEx(handle); IOWrapper.DiskCreateDiskMBR(handle, 0xa5a5a5); IOWrapper.DiskUpdateProperties(handle); x = IOWrapper.DiskGetDriveLayoutEx(handle); x.PartitionEntry[0].PartitionNumber = 1; x.PartitionEntry[0].StartingOffset = 4 * 512; x.PartitionEntry[0].PartitionLength = (long)dd.GetDriveSize() - x.PartitionEntry[0].StartingOffset; x.PartitionEntry[0].RewritePartition = true; x.PartitionEntry[0].Mbr.PartitionType = (byte)IOWrapper.Partition.FAT32; IOWrapper.DiskSetDriveLayoutEx(handle, x); IOWrapper.DiskUpdateProperties(handle); x = IOWrapper.DiskGetDriveLayoutEx(handle); //IOWrapper.StorageLoadMedia(handle); handle.Close(); var driveID = dd.ID; WmiInfo.LoadDiskInfo(); dd = dd.CreationContext.DiskDrives.FirstOrDefault(z => z.ID == driveID); } this.Text += " (Formatting)"; lblSpeed.Text = "Windows is formatting the drive..."; cts = new CancellationTokenSource(); progress.Value = 0; progress.Maximum = 100; //await Task.Run(() => dd.Format(ProgressHandler, cts.Token)); try { var vol = dd.Volumes.FirstOrDefault(); if (vol != null) { vol.Mount(); await Task.Run(() => dd.Volumes.First().Format("FAT32", true), cts.Token); } } catch (Exception ex) { if (!(ex is OperationCanceledException)) { MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error); } cts.Cancel(); } ResetProgress(); FillDriveList(); if (!cts.IsCancellationRequested) { MessageBox.Show("Formatting complete.", "Complete", MessageBoxButtons.OK, MessageBoxIcon.Information); } cts = null; }