Example #1
0
        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;
        }
        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 = string.Format(@"\\.\PHYSICALDRIVE{0}", num.DeviceNumber);

            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);
                }
        }
Example #3
0
 /// <summary>
 /// Instantiates a new instance of <see cref="WindowsDisk"/>.
 /// </summary>
 /// <param name="name">The drive letter.</param>
 public WindowsDisk(string name)
 {
     rootPath = Path.GetPathRoot(Path.GetFullPath(name)).TrimEnd(Path.DirectorySeparatorChar);
     volume   = new DiskDevice(@"\\.\" + rootPath, true);
     try
     {
         DiskDevice.STORAGE_DEVICE_NUMBER devNumber = volume.QueryDeviceNumber();
         disk = new DiskDevice(devNumber.PhysicalDrive, true);
     }
     catch
     {
         volume.Close();
         throw;
     }
 }