Exemplo n.º 1
0
        public override void Close()
        {
            if (handle != smart.InvalidHandle)
            {
                smart.CloseHandle(handle);
            }

            base.Close();
        }
Exemplo n.º 2
0
        public static AbstractHarddrive CreateInstance(ISmart smart, 
      int driveIndex, ISettings settings)
        {
            IntPtr deviceHandle = smart.OpenDrive(driveIndex);

              string name = null;
              string firmwareRevision = null;
              DriveAttributeValue[] values = { };

              if (deviceHandle != smart.InvalidHandle) {
            bool nameValid = smart.ReadNameAndFirmwareRevision(deviceHandle,
            driveIndex, out name, out firmwareRevision);
            bool smartEnabled = smart.EnableSmart(deviceHandle, driveIndex);

            if (smartEnabled)
              values = smart.ReadSmartData(deviceHandle, driveIndex);

            smart.CloseHandle(deviceHandle);

            if (!nameValid) {
              name = null;
              firmwareRevision = null;
            }
              } else {
            string[] logicalDrives = smart.GetLogicalDrives(driveIndex);
            if (logicalDrives == null || logicalDrives.Length == 0)
              return null;
              }

              if (string.IsNullOrEmpty(name))
            name = "Generic Hard Disk";

              if (string.IsNullOrEmpty(firmwareRevision))
            firmwareRevision = "Unknown";

              foreach (Type type in hddTypes) {
            // get the array of name prefixes for the current type
            NamePrefixAttribute[] namePrefixes = type.GetCustomAttributes(
              typeof(NamePrefixAttribute), true) as NamePrefixAttribute[];

            // get the array of the required SMART attributes for the current type
            RequireSmartAttribute[] requiredAttributes = type.GetCustomAttributes(
              typeof(RequireSmartAttribute), true) as RequireSmartAttribute[];

            // check if all required attributes are present
            bool allRequiredAttributesFound = true;
            foreach (var requireAttribute in requiredAttributes) {
              bool adttributeFound = false;
              foreach (DriveAttributeValue value in values) {
            if (value.Identifier == requireAttribute.AttributeId) {
              adttributeFound = true;
              break;
            }
              }
              if (!adttributeFound) {
            allRequiredAttributesFound = false;
            break;
              }
            }

            // if an attribute is missing, then try the next type
            if (!allRequiredAttributesFound)
              continue;

            // check if there is a matching name prefix for this type
            foreach (NamePrefixAttribute prefix in namePrefixes) {
              if (name.StartsWith(prefix.Prefix, StringComparison.InvariantCulture))
            return Activator.CreateInstance(type, smart, name, firmwareRevision,
              driveIndex, settings) as AbstractHarddrive;
            }
              }

              // no matching type has been found
              return null;
        }
Exemplo n.º 3
0
        public static AbstractHarddrive CreateInstance(ISmart smart,
                                                       int driveIndex, ISettings settings)
        {
            IntPtr deviceHandle = smart.OpenDrive(driveIndex);

            string name             = null;
            string firmwareRevision = null;

            DriveAttributeValue[] values = { };

            if (deviceHandle != smart.InvalidHandle)
            {
                bool nameValid = smart.ReadNameAndFirmwareRevision(deviceHandle,
                                                                   driveIndex, out name, out firmwareRevision);
                bool smartEnabled = smart.EnableSmart(deviceHandle, driveIndex);

                if (smartEnabled)
                {
                    values = smart.ReadSmartData(deviceHandle, driveIndex);
                }

                smart.CloseHandle(deviceHandle);

                if (!nameValid)
                {
                    name             = null;
                    firmwareRevision = null;
                }
            }
            else
            {
                string[] logicalDrives = smart.GetLogicalDrives(driveIndex);
                if (logicalDrives == null || logicalDrives.Length == 0)
                {
                    return(null);
                }
            }

            if (string.IsNullOrEmpty(name))
            {
                name = "Generic Hard Disk";
            }

            if (string.IsNullOrEmpty(firmwareRevision))
            {
                firmwareRevision = "Unknown";
            }

            foreach (Type type in hddTypes)
            {
                // get the array of name prefixes for the current type
                NamePrefixAttribute[] namePrefixes = type.GetCustomAttributes(
                    typeof(NamePrefixAttribute), true) as NamePrefixAttribute[];

                // get the array of the required SMART attributes for the current type
                RequireSmartAttribute[] requiredAttributes = type.GetCustomAttributes(
                    typeof(RequireSmartAttribute), true) as RequireSmartAttribute[];

                // check if all required attributes are present
                bool allRequiredAttributesFound = true;
                foreach (var requireAttribute in requiredAttributes)
                {
                    bool adttributeFound = false;
                    foreach (DriveAttributeValue value in values)
                    {
                        if (value.Identifier == requireAttribute.AttributeId)
                        {
                            adttributeFound = true;
                            break;
                        }
                    }
                    if (!adttributeFound)
                    {
                        allRequiredAttributesFound = false;
                        break;
                    }
                }

                // if an attribute is missing, then try the next type
                if (!allRequiredAttributesFound)
                {
                    continue;
                }

                // check if there is a matching name prefix for this type
                foreach (NamePrefixAttribute prefix in namePrefixes)
                {
                    if (name.StartsWith(prefix.Prefix, StringComparison.InvariantCulture))
                    {
                        return(Activator.CreateInstance(type, smart, name, firmwareRevision,
                                                        driveIndex, settings) as AbstractHarddrive);
                    }
                }
            }

            // no matching type has been found
            return(null);
        }
 public override void Close()
 {
     smart.CloseHandle(handle);
     base.Close();
 }