Esempio n. 1
0
        /// <summary>
        /// <para>Compares this <see cref="StorageDisk"/> with another object</para>
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            StorageDisk that = obj as StorageDisk;

            if (that == null)
            {
                return(false);
            }

            return(this.Type == that.Type && this.Name == that.Name);
        }
Esempio n. 2
0
        /// <summary>
        /// <para>Pretty print the disk.</para>
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            StringBuilder builder = new StringBuilder();

            builder.Append(this.DriveLetter);
            builder.Append(": ");
            builder.Append(this.Name);
            builder.Append(" (");
            builder.Append(StorageDisk.FormatByte(this.FreeSpace));
            builder.Append(" free of ");
            builder.Append(StorageDisk.FormatByte(this.Size));
            builder.Append(")");
            return(builder.ToString());
        }
Esempio n. 3
0
        /// <summary>
        /// <para>Gets all available disks of the specified <paramref name="Type"/></para>
        /// </summary>
        /// <param name="Type"></param>
        /// <returns></returns>
        public static List <StorageDisk> GetAvailableDisks(DeviceType Type)
        {
            string query = "SELECT * FROM Win32_DiskDrive";

            if (Type != DeviceType.Any)
            {
                query += " WHERE InterfaceType = '" + Type.ToString() + "'";
            }
            ManagementObjectCollection drives = new ManagementObjectSearcher(query).Get();


            List <StorageDisk> disks = new List <StorageDisk>();

            foreach (ManagementObject drive in drives)                  // browse all queried WMI physical disks
            {
                StorageDisk diskDrive = CreateStorageDiskFromDrive(drive);
                disks.Add(diskDrive);

                ManagementObjectCollection partitions = new ManagementObjectSearcher(String.Format("ASSOCIATORS OF {{Win32_DiskDrive.DeviceID='{0}'}} WHERE AssocClass = Win32_DiskDriveToDiskPartition", drive["DeviceID"])).Get();                     // associate physical disks with partitions

                foreach (ManagementObject partition in partitions)
                {
                    ManagementObjectCollection logicals = new ManagementObjectSearcher(String.Format("ASSOCIATORS OF {{Win32_DiskPartition.DeviceID='{0}'}} WHERE AssocClass = Win32_LogicalDiskToPartition", partition["DeviceID"])).Get();                      // associate partitions with logical disks (drive letter volumes)

                    foreach (ManagementObject logical in logicals)
                    {
                        ManagementObjectCollection volumes = new ManagementObjectSearcher(String.Format("SELECT * FROM Win32_LogicalDisk WHERE Name='{0}'", logical["Name"])).Get();
                        if (volumes.Count == 0)
                        {
                            diskDrive.AddPartition(new Partition(logical));
                        }
                        else
                        {
                            foreach (ManagementObject volume in volumes)
                            {
                                diskDrive.AddPartition(new Partition(volume));
                                break;
                            }
                        }
                    }
                }
            }

            return(disks);
        }
Esempio n. 4
0
        /// <summary>
        /// <para>Pretty print the disk.</para>
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            StringBuilder builder = new StringBuilder();

            builder.Append(this.Type.ToString());
            builder.Append(": ");
            builder.Append(this.Model);
            builder.Append(" (");
            builder.Append(StorageDisk.FormatByte(this.Size));
            builder.Append(" in ");
            builder.Append(this.Partitions.Count);
            builder.Append(" partition");
            if (this.Partitions.Count != 1)
            {
                builder.Append("s");
            }
            builder.Append(")");
            return(builder.ToString());
        }
Esempio n. 5
0
        /// <summary>
        /// <para>Creates a new <see cref="StorageDisk"/> from the specified <paramref name="UnitMask"/></para>
        /// </summary>
        /// <param name="UnitMask"></param>
        /// <returns></returns>
        internal static StorageDisk FromUnitMask(int UnitMask)
        {
            if (UnitMask == 0)
            {
                return(new StorageDisk(null, null, null));
            }

            char        Letter = FirstDriveFromMask(UnitMask);
            StorageDisk sd     = null;

            ManagementObjectCollection partitions = new ManagementObjectSearcher(String.Format("ASSOCIATORS OF {{Win32_LogicalDisk.DeviceID='{0}:'}} WHERE AssocClass = Win32_LogicalDiskToPartition", Letter)).Get();

            foreach (ManagementObject partition in partitions)
            {
                ManagementObjectCollection disks = new ManagementObjectSearcher(String.Format("ASSOCIATORS OF {{Win32_DiskPartition.DeviceID='{0}'}} WHERE AssocClass = Win32_DiskDriveToDiskPartition", partition["DeviceID"])).Get();
                foreach (ManagementObject disk in disks)
                {
                    sd = CreateStorageDiskFromDrive(disk);
                    break;
                }
            }
            if (sd == null)
            {
                ManagementObjectCollection cdroms = new ManagementObjectSearcher(String.Format("SELECT * FROM Win32_CdRomDrive WHERE Drive = '{0}:\'", Letter)).Get();
                foreach (ManagementObject cdrom in cdroms)
                {
                    sd = new CdRomDrive(cdrom);
                    break;
                }
            }

            ManagementObjectCollection volumes = new ManagementObjectSearcher(String.Format("SELECT * FROM Win32_LogicalDisk WHERE DeviceID = '{0}:'", Letter)).Get();

            foreach (ManagementObject volume in volumes)
            {
                sd.AddPartition(new Partition(volume));
                break;
            }
            return(sd);
        }