Example #1
0
        static void Main(string[] args)
        {
            Console.Title = "Demo of Simplified.IO.Smart class";

            try
            {
                var drives = Smart.GetDrives();

                foreach (var drive in drives)
                {
                    Console.WriteLine("-----------------------------------------------------");
                    Console.WriteLine($" DRIVE ({((drive.IsOK) ? "OK" : "BAD")}): {drive.Serial} - {drive.Model} - {drive.Type}");
                    Console.WriteLine("-----------------------------------------------------");
                    Console.WriteLine("");

                    Console.WriteLine("Attribute\t\t\tCurrent  Worst  Threshold  Data  Status");
                    int maxNameLen = drive.SmartAttributes.Max(s => s.Name.Length);
                    foreach (var attr in drive.SmartAttributes)
                    {
                        if (attr.HasData)
                        {
                            Console.WriteLine($"{attr.Name.PadRight(maxNameLen, ' ')} {attr.Current}\t {attr.Worst}\t {attr.Threshold}\t {attr.Data.ToString().PadRight(9, ' ')} {((attr.IsOK) ? "OK" : "BAD")}");
                        }
                    }
                    Console.WriteLine();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("An error occurred: " + e.Message);
            }

            Console.ReadLine();
        }
Example #2
0
        public static void Main()
        {
            try
            {
                //DriveCollection drives = new DriveCollection();

                ////foreach (ManagementObject device in new ManagementObjectSearcher(@"SELECT * FROM Win32_DiskDrive WHERE InterfaceType LIKE 'USB%'").Get())
                //foreach (var device in new ManagementObjectSearcher(@"SELECT * FROM Win32_DiskDrive").Get())
                //{
                //    #region Drive Info
                //    Drive drive = new Drive
                //    {
                //        DeviceID = device.GetPropertyValue("DeviceID").ToString(),
                //        PnpDeviceID = device.GetPropertyValue("PNPDeviceID").ToString(),
                //        Model = device["Model"]?.ToString().Trim(),
                //        Type = device["InterfaceType"]?.ToString().Trim(),
                //        Serial = device["SerialNumber"]?.ToString().Trim()

                //    };
                //    #endregion

                //    #region Get drive letters
                //    foreach (var partition in new ManagementObjectSearcher(
                //        "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" + device.Properties["DeviceID"].Value
                //        + "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition").Get())
                //    {

                //        foreach (var disk in new ManagementObjectSearcher(
                //                    "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='"
                //                        + partition["DeviceID"]
                //                        + "'} WHERE AssocClass = Win32_LogicalDiskToPartition").Get())
                //        {
                //            drive.DriveLetters.Add(disk["Name"].ToString());
                //        }

                //    }
                //    #endregion

                //    #region Overall Smart Status

                //    ManagementScope scope = new ManagementScope("\\\\.\\ROOT\\WMI");
                //    ObjectQuery query = new ObjectQuery(@"SELECT * FROM MSStorageDriver_FailurePredictStatus Where InstanceName like ""%"
                //                                        + drive.PnpDeviceID.Replace("\\", "\\\\") + @"%""");
                //    ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
                //    ManagementObjectCollection queryCollection = searcher.Get();
                //    foreach (ManagementObject m in queryCollection)
                //    {
                //        drive.IsOK = (bool)m.Properties["PredictFailure"].Value == false;
                //    }
                //    #endregion

                //    #region Smart Registers
                //    drive.SmartAttributeAttributes.AddRange(Helper.GetSmartRegisters(Resource.SmartAttributes));

                //    searcher.Query = new ObjectQuery(@"Select * from MSStorageDriver_FailurePredictData Where InstanceName like ""%"
                //                                        + drive.PnpDeviceID.Replace("\\", "\\\\") + @"%""");
                //    foreach (ManagementObject data in searcher.Get())
                //    {
                //        Byte[] bytes = (Byte[]) data.Properties["VendorSpecific"].Value;
                //        foreach (var attribute in drive.SmartAttributeAttributes)
                //        {
                //            try
                //            {
                //                int id = bytes[attribute.Register * 12 + 2];

                //                int flags = bytes[attribute.Register * 12 + 4]; // least significant status byte, +3 most significant byte, but not used so ignored.
                //                bool advisory = (flags & 0x1) == 0x0;
                //                bool failureImminent = (flags & 0x1) == 0x1;
                //                bool onlineDataCollection = (flags & 0x2) == 0x2;

                //                int value = bytes[attribute.Register * 12 + 5];
                //                int worst = bytes[attribute.Register * 12 + 6];
                //                int vendordata = BitConverter.ToInt32(bytes, attribute.Register * 12 + 7);
                //                if (id == 0) continue;

                //                attribute.Current = value;
                //                attribute.Worst = worst;
                //                attribute.Data = vendordata;
                //                attribute.IsOK = failureImminent == false;

                //                //Console.WriteLine("{0}\t {1}\t {2}\t {3}\t " + attribute.Data + " " + ((attribute.IsOK) ? "OK" : ""), attribute.Name, attribute.Current, attribute.Worst, attribute.Threshold);
                //            }
                //            catch (Exception ex)
                //            {
                //                Debug.WriteLine($"Error resolving attribute data [{attribute.Name}].");
                //            }
                //        }
                //    }

                //    searcher.Query = new ObjectQuery(@"Select * from MSStorageDriver_FailurePredictThresholds Where InstanceName like ""%"
                //                                    + drive.PnpDeviceID.Replace("\\", "\\\\") + @"%""");
                //    foreach (ManagementObject data in searcher.Get())
                //    {
                //        Byte[] bytes = (Byte[])data.Properties["VendorSpecific"].Value;
                //        for (int i = 0; i < 30; ++i)
                //        {
                //            try
                //            {
                //                int id = bytes[i * 12 + 2];
                //                int thresh = bytes[i * 12 + 3];
                //                if (id == 0) continue;

                //                var attr = drive.SmartAttributeAttributes.GetAttribute(id);
                //                attr.Threshold = thresh;

                //                //Console.WriteLine("{0}\t {1}\t {2}\t {3}\t " + attr.Data + " " + ((attr.IsOK) ? "OK" : ""), attr.Name, attr.Current, attr.Worst, attr.Threshold);
                //            }
                //            catch
                //            {
                //                // given key does not exist in attribute collection (attribute not in the dictionary of attributes)
                //            }
                //        }
                //    }
                //    #endregion

                //    drives.Add(drive);
                //}

                var drives = Smart.GetDrives();

                // print
                foreach (var drive in drives)
                {
                    Console.WriteLine("-----------------------------------------------------");
                    Console.WriteLine(" DRIVE ({0}): " + drive.Serial + " - " + drive.Model + " - " + drive.Type, ((drive.IsOK) ? "OK" : "BAD"));
                    Console.WriteLine("-----------------------------------------------------");
                    Console.WriteLine("");

                    Console.WriteLine("ID                   Current  Worst  Threshold  Data  Status");
                    foreach (var attr in drive.SmartAttributeAttributes)
                    {
                        if (attr.HasData)
                        {
                            Console.WriteLine("{0}\t {1}\t {2}\t {3}\t " + attr.Data + " " + ((attr.IsOK) ? "OK" : "BAD"), attr.Name, attr.Current, attr.Worst, attr.Threshold);
                        }
                    }
                    Console.WriteLine();
                    Console.WriteLine();
                    Console.WriteLine();
                }

                Console.ReadLine();
            }
            catch (ManagementException e)
            {
                Console.WriteLine("An error occurred while querying for WMI data: " + e.Message);
            }
        }