C# 獲取磁碟空間或者可用空間
阿新 • • 發佈:2019-02-10
/// <summary>
/// 獲取指定驅動器的空間總大小(單位為G)
/// 只需輸入代表驅動器的字母即可
/// </summary>
/// <param name="DiskName"></param>
/// <returns></returns>
public static double GetHardDiskSpace(string DiskName)
{
double totalSize = new double();
DiskName = DiskName.ToUpper() + ":\\";
System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
foreach (System.IO.DriveInfo drive in drives)
{
if (drive.Name == DiskName)
{
totalSize = drive.TotalSize / (double)(1024 * 1024 * 1024);
}
}
return totalSize;
}
/// <summary>
/// 獲取指定驅動器的剩餘空間總大小(單位為G)
/// 只需輸入代表驅動器的字母即可
/// </summary>
/// <param name="DiskName"></param>
/// <returns></returns>
public static double GetHardDiskFreeSpace(string DiskName)
{
double freeSpace = new double();
DiskName = DiskName.ToUpper() + ":\\";
System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
foreach (System.IO.DriveInfo drive in drives)
{
if (drive.Name == DiskName)
{
freeSpace = drive.TotalFreeSpace / (double)(1024 * 1024 * 1024);
}
}
return freeSpace;
}
/// 獲取指定驅動器的空間總大小(單位為G)
/// 只需輸入代表驅動器的字母即可
/// </summary>
/// <param name="DiskName"></param>
/// <returns></returns>
public static double GetHardDiskSpace(string DiskName)
{
double totalSize = new double();
DiskName = DiskName.ToUpper() + ":\\";
System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
foreach (System.IO.DriveInfo drive in drives)
{
if (drive.Name == DiskName)
{
totalSize = drive.TotalSize / (double)(1024 * 1024 * 1024);
}
}
return totalSize;
}
/// <summary>
/// 獲取指定驅動器的剩餘空間總大小(單位為G)
/// 只需輸入代表驅動器的字母即可
/// </summary>
/// <param name="DiskName"></param>
/// <returns></returns>
public static double GetHardDiskFreeSpace(string DiskName)
{
double freeSpace = new double();
DiskName = DiskName.ToUpper() + ":\\";
System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
foreach (System.IO.DriveInfo drive in drives)
{
if (drive.Name == DiskName)
{
freeSpace = drive.TotalFreeSpace / (double)(1024 * 1024 * 1024);
}
}
return freeSpace;
}