public static void ReadProperties(Computer computer)
        {
            try
            {
                Console.WriteLine("Starting reading Logical Drives properties...");

                using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDisk"))
                {
                    foreach (var drive in searcher.Get())
                    {
                        var logicalDrive = new LogicalDrive();

                        logicalDrive.Name         = drive["Caption"].AsString();
                        logicalDrive.SerialNumber = drive["VolumeSerialNumber"].AsString();
                        logicalDrive.FileSystem   = drive["FileSystem"].AsString();
                        logicalDrive.Size         = drive["Size"].AsGigabyte();
                        logicalDrive.FreeSpace    = drive["FreeSpace"].AsGigabyte();

                        computer.LogicalDrives.Add(logicalDrive);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error while reading Logical Drivers properties: {ex.ToString()}");
            }
        }
Пример #2
0
        /// <summary>
        /// Compares the current instance with another object of the same type.
        /// </summary>
        /// <param name="obj">An object to compare with this instance.</param>
        /// <returns>A 32-bit signed integer that indicates the relative order of the comparands.</returns>
        public override int CompareTo(object obj)
        {
            Volume device = obj as Volume;

            if (device == null)
            {
                throw new ArgumentException();
            }

            if (LogicalDrive == null)
            {
                return(1);
            }

            if (device.LogicalDrive == null)
            {
                return(-1);
            }

            return(LogicalDrive.CompareTo(device.LogicalDrive));
        }
Пример #3
0
            /// <summary>
            /// class constructor
            /// </summary>
            public LogicalDrives()
            {
                try
                {
                    ManagementObjectSearcher drv = new ManagementObjectSearcher("select * from Win32_LogicalDisk");

                    if (drv.Get().Count == -1)
                    {
                        throw new Exception("Could not get Drive(s) information.");
                    }



                    // Iterate over all logical drives
                    foreach (ManagementObject ObjDrv in drv.Get())
                    {
                        LogicalDrive Drv_Info = new LogicalDrive();
                        //Iterate over each logical drive informations
                        foreach (PropertyData PC in ObjDrv.Properties)
                        {
                            if (PC.Value != null)
                            {
                                if (PC.Name == "FileSystem")
                                {
                                    Drv_Info.Drive_File_System = (string)ObjDrv[PC.Name];
                                }

                                if (PC.Name == "Name")
                                {
                                    Drv_Info.Drive_Name = (string)ObjDrv[PC.Name];
                                }

                                if (PC.Name == "VolumeName")
                                {
                                    Drv_Info.Volume_Name = (string)ObjDrv[PC.Name];
                                }

                                if (PC.Name == "VolumeSerialNumber")
                                {
                                    Drv_Info.Drive_Serial_Number = (string)ObjDrv[PC.Name];
                                }

                                if (PC.Name == "Size")
                                {
                                    Drv_Info.Drive_Size = Convert.ToUInt64(ObjDrv[PC.Name]);
                                }

                                if (PC.Name == "DriveType")
                                {
                                    Drv_Info.Drive_Type = Convert.ToUInt16(ObjDrv[PC.Name]);
                                }

                                if (PC.Name == "FreeSpace")
                                {
                                    Drv_Info.Free_Space = Convert.ToUInt64(ObjDrv[PC.Name]);
                                }

                                if (PC.Name == "Compressed")
                                {
                                    Drv_Info.Is_Drive_Compressed = (bool)ObjDrv[PC.Name];
                                }

                                Drv_Info.Used_Space = Convert.ToUInt64(Drv_Info.Drive_Size - Drv_Info.Free_Space);
                            }
                        }

                        // add current logical drive to the logical drives list
                        Logical_drives.Add(Drv_Info);
                    }
                }

                catch (Exception ex)
                {
                    throw ex;
                }
            }