1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- 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
- {
- /// <summary>
- /// 获取硬盘上所有的盘符空间信息列表
- /// </summary>
- /// <returns></returns>
- public static 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 ex)
- {
- LogHelper.log.Error(string.Format("获取磁盘空间信息失败,错误为{0}", ex.ToString()));
- }
- return list;
- }
- }
- }
|