/// <summary> /// 获取硬盘上所有的盘符空间信息列表 /// </summary> /// <returns></returns> public List <HardDiskPartition> GetDiskListInfo() { List <HardDiskPartition> list = null; //指定分区的容量信息 try { SelectQuery selectQuery = new SelectQuery("select * from win32_logicaldisk"); ManagementObjectSearcher searcher = new ManagementObjectSearcher(selectQuery); ManagementObjectCollection diskcollection = searcher.Get(); if (diskcollection != null && diskcollection.Count > 0) { list = new List <HardDiskPartition>(); HardDiskPartition harddisk = null; foreach (ManagementObject disk in searcher.Get()) { int nType = Convert.ToInt32(disk["DriveType"]); if (nType != Convert.ToInt32(DriveType.Fixed)) { continue; } else { harddisk = new HardDiskPartition(); harddisk.FreeSpace = Convert.ToDouble(disk["FreeSpace"]) / (1024 * 1024 * 1024); harddisk.SumSpace = Convert.ToDouble(disk["Size"]) / (1024 * 1024 * 1024); harddisk.PartitionName = disk["DeviceID"].ToString(); list.Add(harddisk); } } } } catch (Exception) { } return(list); }
/// <summary> /// 获取硬盘上指定名称的盘符空间信息列表 /// </summary> /// <returns></returns> public HardDiskPartition GetDiskInfo(string diskname) { //指定分区的容量信息 HardDiskPartition harddisk = null; try { SelectQuery selectQuery = new SelectQuery("select * from win32_logicaldisk"); ManagementObjectSearcher searcher = new ManagementObjectSearcher(selectQuery); ManagementObjectCollection diskcollection = searcher.Get(); if (diskcollection != null && diskcollection.Count > 0) { foreach (ManagementObject disk in searcher.Get()) { int nType = Convert.ToInt32(disk["DriveType"]); if (nType != Convert.ToInt32(DriveType.Fixed)) { continue; } else { if (disk["DeviceID"].ToString().ToUpper() == diskname.ToUpper()) { harddisk = new HardDiskPartition(); harddisk.FreeSpace = Convert.ToDouble(disk["FreeSpace"]) / (1024 * 1024 * 1024); harddisk.SumSpace = Convert.ToDouble(disk["Size"]) / (1024 * 1024 * 1024); harddisk.PartitionName = disk["DeviceID"].ToString(); break; } } } } } catch (Exception) { harddisk = null; } return(harddisk); }