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