public static Dictionary <int, string> GetVolumesForPhysicalDisk(int deviceNumber) { StringBuilder volName = new StringBuilder(260); IntPtr hVolume = FindFirstVolume(volName, volName.Capacity); var result = new Dictionary <int, string>(); if (hVolume == (IntPtr)(-1)) { return(result); } do { try { using (var dev = new DiskDevice(volName.ToString().TrimEnd('\\'), true)) { var dn = dev.QueryDeviceNumber(); if (dn == null || dn.DeviceNumber != deviceNumber) { continue; } result[dn.PartitionNumber] = volName.ToString(); } } catch { } } while (FindNextVolume(hVolume, volName, volName.Capacity)); FindVolumeClose(hVolume); return(result); }
private void button3_Click(object sender, EventArgs e) { DiskDevice dev = null; FileStream fs = null; try { if (!File.Exists(txtFileName.Text)) { throw new Exception("Please select a valid image file"); } if (lvDevices.SelectedItems.Count == 0) { throw new Exception("Please select target device"); } var rec = lvDevices.SelectedItems[0].Tag as DiskRecord; var info = rec?.Info; if (info == null) { throw new Exception("Please select a valid target device"); } string devPath = info.DevicePath; dev = new DiskDevice(devPath); long volumeSize = 0; byte[] result = new byte[8]; if (dev.DeviceIoControl(IOCTL_DISK_GET_LENGTH_INFO, null, result) == 8) { volumeSize = BitConverter.ToInt64(result, 0); } if (volumeSize <= 0) { throw new Exception("Please insert a card into the card reader"); } fs = File.Open(txtFileName.Text, FileMode.Open, FileAccess.Read, FileShare.Read); if (fs.Length > volumeSize) { throw new Exception(string.Format("The selected media ({0}) is too small for the selected image file {1})", StringHelpers.FormatByteCount(volumeSize), StringHelpers.FormatByteCount(fs.Length))); } _RegHelper.SetValue("LastImageFile", txtFileName.Text); var number = dev.QueryDeviceNumber(); if (new EraseConfirmationForm(info, dev, volumeSize).ShowDialog() != DialogResult.OK) { return; } lblProgress.Text = "Preparing..."; GUIEnabled = false; _AbortWriting = false; var ctx = new ThreadContext { VolumeSize = volumeSize, DeviceNumber = number, fs = fs, devID = info.DeviceID, FileName = txtFileName.Text }; if (cbResize.Checked) { try { var pt = ParsedChangeFile.ReadPartitionTable(ctx.FileName); if (pt != null && pt.Length > 0 && pt[pt.Length - 1].Type == 0x83) { ctx.ResizedPartition = pt[pt.Length - 1]; } } catch { } } new Thread(WriteThreadBody).Start(ctx); fs = null; dev = null; } catch (System.Exception ex) { MessageBox.Show(ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { fs?.Dispose(); dev?.Dispose(); } }
public EraseConfirmationForm(DeviceEnumerator.DeviceInfo info, DiskDevice dev, long volumeSize) { InitializeComponent(); this.info = info; lblDevName.Text = info.UserFriendlyName; lblDevSize.Text = StringHelpers.FormatByteCount(volumeSize) + "B"; var num = dev.QueryDeviceNumber(); if (num == null) { lblInternalName.Text = "Unknown"; } else { lblInternalName.Text = num.PhysicalDrive; } Dictionary <int, string> volumesByPartitionNumbers = null; try { if (num != null) { volumesByPartitionNumbers = VolumeManager.GetVolumesForPhysicalDisk(num.DeviceNumber); } } catch { } var layout = dev.QueryLayoutInformation(); if (layout != null) { for (int i = 0; i < layout.PartitionCount; i++) { if (layout.PartitionEntry[i].PartitionType == 0) { continue; } ListViewItem lvi = new ListViewItem((i + 1).ToString()); lvi.SubItems.Add(StringHelpers.FormatByteCount(layout.PartitionEntry[i].StartingOffset) + "B"); lvi.SubItems.Add(StringHelpers.FormatByteCount(layout.PartitionEntry[i].PartitionLength) + "B"); lvi.SubItems.Add(MapPartitionType(layout.PartitionEntry[i].PartitionType)); string volID; bool found = false; if (volumesByPartitionNumbers != null && volumesByPartitionNumbers.TryGetValue(layout.PartitionEntry[i].PartitionNumber, out volID)) { volumesByPartitionNumbers.Remove(layout.PartitionEntry[i].PartitionNumber); string mountPoints = VolumeManager.GetVolumeMountPoints(volID, '|'); if (mountPoints != null) { lvi.Tag = mountPoints.Split('|')[0]; lvi.SubItems.Add(mountPoints.Replace("|", "; ")); found = mountPoints.Length > 0; } } lvi.ImageIndex = found ? 0 : 1; lvPartitions.Items.Add(lvi); } } }