public void enumerateFATDevices() { disks = new List <HardDrive>(); ManagementObjectSearcher search = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive"); //Iterate over each search result - list of HD's from WMI foreach (ManagementObject wmi_HD in search.Get()) { HardDrive hdd = new HardDrive(); hdd.Model = wmi_HD["Model"].ToString(); hdd.Type = wmi_HD["InterfaceType"] == null ? "" : wmi_HD["InterfaceType"].ToString(); hdd.DeviceId = wmi_HD["DeviceId"].ToString(); BufferedDiskReader disk = new BufferedDiskReader(hdd.DeviceId); //Occurs when in use or insufficient privileges if (disk.IsInvalid) { int error = Marshal.GetLastWin32Error(); MessageBox.Show(this, "Please verify you have Administrator Privileges and disks are not in use.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop); continue; } //HDD is valid, add to our list disks.Add(hdd); //Sector buffer byte[] data = new byte[512]; //Fill Buffer disk.Read(data, 0, 512); //Deserialize data into MasterBootRecord hdd.MBR = new MasterBootRecord(data); //Iterate over partitions in MBR foreach (PartitionTableEntry entry in hdd.MBR.PartitionTable.Partitions) { //Clear data buffer data = new byte[512]; //Seek to Partition start (FAT32 boot sector, location given in Partition table entry in Sectors) disk.SeekAbsolute((ulong)entry.LBA_Begin1 * BYTES_PER_SECTOR); //Read FAT32 BootSector - Volume Info disk.Read(data, 0, data.Length); //Deserialize data into Partition object hdd.Partitions.Add(new Partition(data, hdd, entry)); } //Got to remember to close the disk disk.Close(); } }
/* * CTOR - Creates FATBootSector and Calculates Partition Cluster Start LBA * */ public Partition(byte[] bootSectorBytes, HardDrive hdd, PartitionTableEntry entry) { if (entry.TypeCode == 0x0B || entry.TypeCode == 0x0C) { bootSector = new FATBootSector(bootSectorBytes); //Partition cluster starts immediately following both FAT's clusterBeginLBA = entry.LBA_Begin1 + bootSector.BPB.ReservedSectors + (bootSector.BPB.NumOfFATs * bootSector.BPB.SectorsPerFAT32); fatBeginLBA = entry.LBA_Begin1 + bootSector.BPB.ReservedSectors; } this.hdd = hdd; this.entry = entry; }
public void enumerateFATDevices() { disks = new List<HardDrive>(); ManagementObjectSearcher search = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive"); //Iterate over each search result - list of HD's from WMI foreach (ManagementObject wmi_HD in search.Get()) { HardDrive hdd = new HardDrive(); hdd.Model = wmi_HD["Model"].ToString(); hdd.Type = wmi_HD["InterfaceType"] == null ? "" : wmi_HD["InterfaceType"].ToString(); hdd.DeviceId = wmi_HD["DeviceId"].ToString(); BufferedDiskReader disk = new BufferedDiskReader(hdd.DeviceId); //Occurs when in use or insufficient privileges if (disk.IsInvalid) { int error = Marshal.GetLastWin32Error(); MessageBox.Show(this, "Please verify you have Administrator Privileges and disks are not in use.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop); continue; } //HDD is valid, add to our list disks.Add(hdd); //Sector buffer byte[] data = new byte[512]; //Fill Buffer disk.Read(data, 0, 512); //Deserialize data into MasterBootRecord hdd.MBR = new MasterBootRecord(data); //Iterate over partitions in MBR foreach (PartitionTableEntry entry in hdd.MBR.PartitionTable.Partitions) { //Clear data buffer data = new byte[512]; //Seek to Partition start (FAT32 boot sector, location given in Partition table entry in Sectors) disk.SeekAbsolute((ulong)entry.LBA_Begin1 * BYTES_PER_SECTOR); //Read FAT32 BootSector - Volume Info disk.Read(data, 0, data.Length); //Deserialize data into Partition object hdd.Partitions.Add(new Partition(data, hdd, entry)); } //Got to remember to close the disk disk.Close(); } }