Ejemplo n.º 1
0
        public static List <Disk> GetDisks(DiskType type = DiskType.All)
        {
            List <Disk> disks = new List <Disk>();

            string[] filter = new string[] { "manufacturer", "mediatype", "model", "partitions", "serialnumber" };

            foreach (ManagementObject disk in new ManagementObjectSearcher("Select * From Win32_DiskDrive").Get())
            {
                if (type == DiskType.All || disk["MediaType"].ToString().ToLowerInvariant().Contains(type.ToString().ToLowerInvariant()))
                {
                    Disk currentDisk = new Disk(disk["DeviceID"].ToString());

                    foreach (PropertyData property in disk.Properties)
                    {
                        string name = property.Name.ToLowerInvariant();
                        if (name == "deviceid")
                        {
                            continue;
                        }
                        if (name == "size")
                        {
                            currentDisk.Size = (UInt64)property.Value;
                        }
                        else if (filter.Contains(name))
                        {
                            currentDisk.AddProperty(property);
                        }
                    }

                    // add associated disk partitions
                    currentDisk.Partitions = DiskPartition.GetDiskPartitions(currentDisk.DeviceID);

                    disks.Add(currentDisk);
                }
            }

            return(disks);
        }
Ejemplo n.º 2
0
        internal static List <DiskPartition> GetDiskPartitions(string deviceID)
        {
            List <DiskPartition> partitions = new List <DiskPartition>();

            string[] filter = new string[] { "bootable", "bootpartition", "diskindex", "index", "name", "primarypartition", "type" };

            foreach (ManagementObject partition in new ManagementObjectSearcher(String.Format(
                                                                                    "associators of {{Win32_DiskDrive.DeviceID='{0}'}} " +
                                                                                    "where AssocClass = Win32_DiskDriveToDiskPartition", deviceID)).Get())
            {
                DiskPartition currentPartition = new DiskPartition(partition["DeviceID"].ToString());

                foreach (PropertyData property in partition.Properties)
                {
                    string name = property.Name.ToLowerInvariant();
                    if (name == "deviceid")
                    {
                        continue;
                    }
                    if (name == "size")
                    {
                        currentPartition.Size = (UInt64)property.Value;
                    }
                    else if (filter.Contains(name))
                    {
                        currentPartition.AddProperty(property);
                    }
                }

                currentPartition.LogicalDisk = LogicalDisk.GetLogicalDisk(currentPartition.DeviceID);

                partitions.Add(currentPartition);
            }

            return(partitions);
        }