drive.TotalFreeSpace單位爲bit,根據須要除以1024
drive同時能夠能夠獲取磁盤分區容量等
//單位MB public static long GetHardDiskSpace(string str_HardDiskName) { long totalSize = 0; 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.TotalFreeSpace / (1024 * 1024); } } return totalSize; }
調用方法:spa
string AppPath = Application.StartupPath.ToString(); string volume = AppPath.Substring(0, AppPath.IndexOf(':')); long freespace = GetHardDiskSpace(volume);