using ProjectBase.Data.Logs;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;
namespace ProjectBase.Data.hardDisk
{
public class HardDiskInfo
{
///
/// 获取硬盘上所有的盘符空间信息列表
///
///
public static List GetDiskListInfo()
{
List 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 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 ex)
{
LogHelper.log.Error(string.Format("获取磁盘空间信息失败,错误为{0}", ex.ToString()));
}
return list;
}
}
}