public static List <HardDrive> GetHardDriveData() { List <HardDrive> hardDriveList = new List <HardDrive>(); ManagementScope hardDriveScope = new ManagementScope(); ObjectQuery objectQuery = new ObjectQuery("select FreeSpace,Size,Name from Win32_LogicalDisk where DriveType=3"); ManagementObjectSearcher hardDriveObjectSearcher = new ManagementObjectSearcher(hardDriveScope, objectQuery); ManagementObjectCollection hardDriveObjectCollection = hardDriveObjectSearcher.Get(); foreach (ManagementObject hardDrive in hardDriveObjectCollection) { string name = hardDrive["Name"].ToString(); long freeSpace = long.Parse(hardDrive["FreeSpace"].ToString()); long diskSize = long.Parse(hardDrive["Size"].ToString()); HardDrive hardDriveObject = new HardDrive(name, freeSpace, diskSize); hardDriveList.Add(hardDriveObject); } return(hardDriveList); }
public static List <HardDrive> GetHardDiskSerialNumber(string drive) { List <HardDrive> hardDriveSerialNumberList = new List <HardDrive>(); ManagementObject managementObject = new ManagementObject("Win32_LogicalDisk.DeviceID=\"" + drive + ":\""); managementObject.Get(); string serialNumber = managementObject["VolumeSerialNumber"].ToString(); HardDrive addSerialNumber = new HardDrive(serialNumber); hardDriveSerialNumberList.Add(addSerialNumber); return(hardDriveSerialNumberList); }