Пример #1
0
        public List<StorageDTOResponse> GetStorageInfo()
        {
            List<StorageDTOResponse> storage = new List<StorageDTOResponse>();

            try
            {
                SelectQuery diskDrivesQuery = new SelectQuery("select Caption, DeviceID, SerialNumber from Win32_DiskDrive");
                ManagementObjectSearcher diskDrivesSearcher = new ManagementObjectSearcher(diskDrivesQuery);

                foreach (ManagementObject diskDrive in diskDrivesSearcher.Get())
                {
                    RelatedObjectQuery diskPartitionsQuery = new RelatedObjectQuery(
                        "associators of {Win32_DiskDrive.DeviceID='" +
                        GetValueAsString(diskDrive, "DeviceID") +
                        "'} where AssocClass=Win32_DiskDriveToDiskPartition"
                    );
                    ManagementObjectSearcher diskPartitionsSearcher = new ManagementObjectSearcher(diskPartitionsQuery);

                    foreach (ManagementObject diskPartition in diskPartitionsSearcher.Get())
                    {
                        RelatedObjectQuery logicalDisksQuery = new RelatedObjectQuery(
                            "associators of {Win32_DiskPartition.DeviceID='" +
                            GetValueAsString(diskPartition, "DeviceID") +
                            "'} where AssocClass=Win32_LogicalDiskToPartition"
                        );
                        ManagementObjectSearcher logicalDisksSearcher = new ManagementObjectSearcher(logicalDisksQuery);

                        foreach (ManagementObject logicalDisk in logicalDisksSearcher.Get())
                        {
                            StorageDTOResponse disk = new StorageDTOResponse();
                            disk.Label = GetValueAsString(diskDrive, "Caption");
                            disk.MountPoint = GetValueAsString(logicalDisk, "Caption");
                            try
                            {
                                disk.Size = ConvertSizeToMiB(Int64.Parse(logicalDisk["Size"].ToString()), SizeUnits.B).ToString();
                            }
                            catch (Exception e)
                            {
                                Logger.Instance.LogError(e.ToString());
                            }
                            disk.Sn = GetValueAsString(diskDrive, "SerialNumber");

                            storage.Add(disk);
                        }
                    }
                }
            }
            catch (ManagementException e)
            {
                Logger.Instance.LogError(e.ToString());
            }

            return storage;
        }
Пример #2
0
		public List<StorageDTOResponse> GetStorageInfo()
		{
			List<StorageDTOResponse> storage = new List<StorageDTOResponse>();

			try
			{
				// In Windows <= Windows Server 2003 Win32_DiskDrive doesn't have SerialNumber field.
				string query;
				if (osVersionNumber < 6)
				{
					query = "select Caption, DeviceID, Model from Win32_DiskDrive";
				}
				else
				{
					query = "select Caption, DeviceID, SerialNumber, Model from Win32_DiskDrive";
				}
				
				SelectQuery diskDrivesQuery = new SelectQuery(query);
				ManagementObjectSearcher diskDrivesSearcher = new ManagementObjectSearcher(diskDrivesQuery);
				
				foreach (ManagementObject diskDrive in diskDrivesSearcher.Get())
				{
					string sn = "";
					if (osVersionNumber < 6)
					{
						// In Windows <= Windows Server 2003 we can find SerialNumber in Win32_PhysicalMedia.
						SelectQuery snQuery = new SelectQuery("select SerialNumber from Win32_PhysicalMedia where tag='" + GetValueAsString(diskDrive, "DeviceID").Replace(@"\", @"\\") + "'");
						ManagementObjectSearcher snSearcher = new ManagementObjectSearcher(snQuery);
						
						foreach (ManagementObject snObj in snSearcher.Get())
						{
							sn = GetValueAsString(snObj, "SerialNumber");
							break;
						}
					}
					else
					{
						sn = GetValueAsString(diskDrive, "SerialNumber");
					}
					
					if (sn.Length == 0 ||
						sn.StartsWith("QM000") ||
					    Blacklists.IsDiscVendorInBlacklist(GetValueAsString(diskDrive, "Caption").ToLower()) ||
					    Blacklists.IsDiskProductInBlacklist(GetValueAsString(diskDrive, "Model").ToLower())
					)
					{
						continue;
					}
					
					RelatedObjectQuery diskPartitionsQuery = new RelatedObjectQuery(
						"associators of {Win32_DiskDrive.DeviceID='" +
						GetValueAsString(diskDrive, "DeviceID") +
						"'} where AssocClass=Win32_DiskDriveToDiskPartition"
					);
					ManagementObjectSearcher diskPartitionsSearcher = new ManagementObjectSearcher(diskPartitionsQuery);
					
					foreach (ManagementObject diskPartition in diskPartitionsSearcher.Get())
					{
						RelatedObjectQuery logicalDisksQuery = new RelatedObjectQuery(
							"associators of {Win32_DiskPartition.DeviceID='" +
							GetValueAsString(diskPartition, "DeviceID") +
							"'} where AssocClass=Win32_LogicalDiskToPartition"
						);
						ManagementObjectSearcher logicalDisksSearcher = new ManagementObjectSearcher(logicalDisksQuery);
						
						foreach (ManagementObject logicalDisk in logicalDisksSearcher.Get())
						{
							StorageDTOResponse disk = new StorageDTOResponse();
							disk.Label = GetValueAsString(diskDrive, "Caption");
							disk.MountPoint = GetValueAsString(diskDrive, "DeviceID");
							try
							{
								disk.Size = ConvertSizeToMiB(Int64.Parse(logicalDisk["Size"].ToString()), SizeUnits.B).ToString();
							}
							catch (Exception e)
							{
								Logger.Instance.LogError(e.ToString());
							}
							disk.SerialNumber = sn;
							disk.Family = GetValueAsString(diskDrive, "Model");
							
							storage.Add(disk);
						}
					}
				}
			}
			catch (ManagementException e)
			{
				Logger.Instance.LogError(e.ToString());
			}
			
			return storage;
		}