/// <summary> /// 获取硬盘容量 /// </summary> public ComputerInfo GetDiskSize() { ComputerInfo computer = null; try { computer = new ComputerInfo(); var diskcon = Directory.GetLogicalDrives(); long total = 0; long free = 0; foreach (string diskname in diskcon) { try { DriveInfo di = new DriveInfo(diskname); if (di.IsReady) { total += di.TotalSize; free += di.AvailableFreeSpace; computer.DiskCount++; } } catch { } } computer.DiskCount = diskcon.Length; computer.RAMTotal = CommHelper.ToGB(total, 1000); computer.RAMFree = CommHelper.ToGB(free, 1000); computer.RAMUsed = CommHelper.ToGB(total - free, 1000); computer.UsedRate = (100 - Math.Ceiling((decimal)free * 100 / total)).ToString() + "%"; }catch (Exception ex) { LoggerHelper.Exception(ex); } return(computer); }
/// <summary> /// 获取硬盘容量 /// </summary> private string GetDiskSize1() { string result = string.Empty; StringBuilder sb = new StringBuilder(); try { string hdId = string.Empty; ManagementClass hardDisk = new ManagementClass("win32_DiskDrive"); ManagementObjectCollection hardDiskC = hardDisk.GetInstances(); foreach (ManagementObject m in hardDiskC) { long capacity = Convert.ToInt64(m["Size"].ToString()); sb.Append(CommHelper.ToGB(capacity, 1000.0) + "+"); } result = sb.ToString().TrimEnd('+'); } finally { } return(result); }