C#.Net獲取磁盤的空間大小

下面直接上代碼


 private void button1_Click(object sender, EventArgs e)
        {
            string strRecordFilePath = "d:/dasda";
            string str=strRecordFilePath.Substring(0, 1).ToUpper();//取首字母并轉(zhuǎn)為大寫
            this.m_ChkDisk(str);
        }

    //strDisk應(yīng)為大寫,如 "D"

        private void m_ChkDisk(string strDisk)
        {
            if (strDisk == "")
                return;

            txtTotal.Text = GetHardDiskSpace(strDisk).ToString();
            txtFreeSpace.Text = GetHardDiskFreeSpace(strDisk).ToString();
            int a, b, c;
            a = Convert.ToInt32(txtTotal.Text);
            b = Convert.ToInt32(txtFreeSpace.Text);
            c = a - b;
            
            txtUse.Text=c.ToString();
        }

        ///  <summary> 
        /// 獲取指定驅(qū)動器的空間總大小(單位為B) 
        ///  </summary> 
        ///  <param name="str_HardDiskName">只需輸入代表驅(qū)動器的字母即可 (大寫)</param> 
        ///  <returns> </returns> 
        public static long GetHardDiskSpace(string str_HardDiskName) 
        { 
            long totalSize= new long(); 
            str_HardDiskName=str_HardDiskName  ":\\"; 
            System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives(); 
            foreach (System.IO.DriveInfo drive in drives) 
            { 
                if (drive.Name == str_HardDiskName) 
                {
                    totalSize = drive.TotalSize / (1024 * 1024 * 1024); 
                } 
            } 
            return totalSize; 
        }

        ///  <summary> 
        /// 獲取指定驅(qū)動器的剩余空間總大小(單位為B) 
        ///  </summary> 
        ///  <param name="str_HardDiskName">只需輸入代表驅(qū)動器的字母即可 </param> 
        ///  <returns> </returns> 
        public static long GetHardDiskFreeSpace(string str_HardDiskName) 
        { 
            long freeSpace = new long(); 
            str_HardDiskName = str_HardDiskName   ":\\"; 
            System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives(); 
            foreach (System.IO.DriveInfo drive in drives) 
            { 
                if (drive.Name == str_HardDiskName) 
                {
                    freeSpace = drive.TotalFreeSpace / (1024 * 1024 * 1024); 
                } 
            } 
            return freeSpace; 
        }


原文鏈接:C#.Net獲取磁盤的空間大小