2021-3-17 获取当前磁盘空间大小及实例
获取当前程序源路径下的磁盘空间
Helper类的定义
public class Disk { public void GetHardDiskFreeSpace(string HardDiskName, ref long All, ref long Free) { HardDiskName = HardDiskName + ":\\"; System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives(); foreach (System.IO.DriveInfo drive in drives) { if (drive.Name == HardDiskName) { All = drive.TotalSize / 1073741824; //总容量 Free = drive.TotalFreeSpace / 1073741824;//剩余容量 } } } }
代码运行实例1
private void SelectBtn_Click(object sender, EventArgs e) { Disk disk = new Disk(); long all = 0; long free = 0; disk.GetHardDiskFreeSpace("D",ref all,ref free); textBox1.Text = free.ToString() + '/' + all.ToString()+'G'; }