HardDiskInfo.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using ProjectBase.Data.Logs;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Management;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace ProjectBase.Data.hardDisk
  10. {
  11. public class HardDiskInfo
  12. {
  13. /// <summary>
  14. /// 获取硬盘上所有的盘符空间信息列表
  15. /// </summary>
  16. /// <returns></returns>
  17. public static List<HardDiskPartition> GetDiskListInfo()
  18. {
  19. List<HardDiskPartition> list = null;
  20. //指定分区的容量信息
  21. try
  22. {
  23. SelectQuery selectQuery = new SelectQuery("select * from win32_logicaldisk");
  24. ManagementObjectSearcher searcher = new ManagementObjectSearcher(selectQuery);
  25. ManagementObjectCollection diskcollection = searcher.Get();
  26. if (diskcollection != null && diskcollection.Count > 0)
  27. {
  28. list = new List<HardDiskPartition>();
  29. HardDiskPartition harddisk = null;
  30. foreach (ManagementObject disk in searcher.Get())
  31. {
  32. int nType = Convert.ToInt32(disk["DriveType"]);
  33. if (nType != Convert.ToInt32(DriveType.Fixed))
  34. {
  35. continue;
  36. }
  37. else
  38. {
  39. harddisk = new HardDiskPartition();
  40. harddisk.FreeSpace = Convert.ToDouble(disk["FreeSpace"]) / (1024 * 1024 * 1024);
  41. harddisk.SumSpace = Convert.ToDouble(disk["Size"]) / (1024 * 1024 * 1024);
  42. harddisk.PartitionName = disk["DeviceID"].ToString();
  43. list.Add(harddisk);
  44. }
  45. }
  46. }
  47. }
  48. catch (Exception ex)
  49. {
  50. LogHelper.log.Error(string.Format("获取磁盘空间信息失败,错误为{0}", ex.ToString()));
  51. }
  52. return list;
  53. }
  54. }
  55. }