C# 獲取磁盤剩余空間


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;
}

  

調用方法:

string AppPath = Application.StartupPath.ToString();
string volume = AppPath.Substring(0, AppPath.IndexOf(':'));
long freespace = GetHardDiskSpace(volume);

  


免責聲明!

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



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