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 btnWrite_Click(object sender, EventArgs e) { DiskDrive dd = (DiskDrive)lstDiskDrive.SelectedItem; if (!CheckFilename(txtFilename.Text, dd, false)) { txtFilename.Focus(); return; } var filename = Path.GetFullPath(txtFilename.Text); if (MessageBox.Show("All data on SD card will be erased and overwritten. Are you sure?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.No) { return; } Operation = "Writing"; if (!TryLockVolumes(dd)) { return; } SetButtons(false); using (Stream source = new FileStream(txtFilename.Text, FileMode.Open, FileAccess.Read, FileShare.Read), dest = dd.CreateStream(FileAccess.Write)) { progress.Maximum = (int)(source.Length / (1024 * 1024)) + 1; progress.Value = 0; cts = new CancellationTokenSource(); try { await CopyStreamAsync(source, dest, source.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(); FillDriveList(); if (!cts.IsCancellationRequested) { MessageBox.Show("Writing complete.", "Complete", MessageBoxButtons.OK, MessageBoxIcon.Information); } ResetProgress(); 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; }