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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM