C# 获取硬盘空间信息 盘符总大小、剩余空间、已用空间

1.如图,项目首先要添加对 System.Management 的引用

------------------------------------------------------------------------------------------------------------------------------------------------------

2.新建hardDiskPartition.cs   盘符信息类

复制代码
 /// 
    /// 盘符信息
    /// 
    public class HardDiskPartition
    {
        #region Data
        private string _PartitionName;
        private double _FreeSpace;
        private double _SumSpace;
        #endregion //Data
 
        #region Properties
        /// 
        /// 空余大小
        /// 
        public double FreeSpace
        {
            get { return _FreeSpace; }
            set { this._FreeSpace = value; }
        }
        /// 
        /// 使用空间
        /// 
        public double UseSpace
        {
            get { return _SumSpace - _FreeSpace; }
        }
        /// 
        /// 总空间
        /// 
        public double SumSpace
        {
            get { return _SumSpace; }
            set { this._SumSpace = value; }
        }
        /// 
        /// 分区名称
        /// 
        public string PartitionName
        {
            get { return _PartitionName; }
            set { this._PartitionName = value; }
        }
        /// 
        /// 是否主分区
        /// 
        public bool IsPrimary
        {
            get
            {
                //判断是否为系统安装分区
                if (System.Environment.GetEnvironmentVariable("windir").Remove(2) == this._PartitionName)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        #endregion //Properties
    }
复制代码

---------------------------------------------------------------------------------------------------------------------------------------------

3.获取盘符空间信息:

复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Management;
using System.IO;
namespace ExPortToExcel
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void btnStart_Click(object sender, EventArgs e)
        {
            try
            {
                List<HardDiskPartition> listInfo = GetDiskListInfo();
                if (listInfo != null && listInfo.Count > 0)
                {
                    listBox1.Items.Clear();
                    foreach(HardDiskPartition disk in listInfo)
                    {
                        listBox1.Items.Add(string.Format("{0}  总空间:{1} GB,剩余:{2} GB", disk.PartitionName, ManagerDoubleValue(disk.SumSpace,1), ManagerDoubleValue(disk.FreeSpace,1)));
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 处理Double值,精确到小数点后几位
        /// </summary>
        /// <param name="_value"></param>
        /// <param name="Length">精确到小数点后几位</param>
        /// <returns>返回值</returns>
        private double ManagerDoubleValue(double _value,int Length)
        {
            if (Length < 0)
            {
                Length = 0;
            }
            return System.Math.Round(_value, Length);
        }
       /// <summary>
       /// 获取硬盘上所有的盘符空间信息列表
       /// </summary>
       /// <returns></returns>
        private 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;
        }
    }
}
复制代码

源码金山快盘下载地址:http://www.kuaipan.cn/index.php?ac=file&oid=18034395877212172


原文连接:http://www.cnblogs.com/luowanli/archive/2012/08/15/2639901.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值