C#实现获取磁盘空间大小的方法

本文实例讲述了C#实现获取磁盘空间大小的方法。分享给大家供大家参考。具体实现方法如下:

方法一:利用System.IO.DriveInfo.GetDrives方法来获取

///   

/// 获取指定驱动器的空间总大小(单位为B)

///  

///  只需输入代表驱动器的字母即可 (大写)

///   

public static long GetHardDiskSpace(string str_HardDiskName)

{

    long totalSize= new long();

    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.TotalSize / (1024 * 1024 * 1024);

 }

    }

    return totalSize;

}

///  

/// 获取指定驱动器的剩余空间总大小(单位为B)

///  

///  只需输入代表驱动器的字母即可 

///   

public static long GetHardDiskFreeSpace(string str_HardDiskName)

{

    long freeSpace = new long();

    str_HardDiskName = str_HardDiskName + ":\\";

    System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();

    foreach (System.IO.DriveInfo drive in drives)

    {

 if (drive.Name == str_HardDiskName)

 {

     freeSpace = drive.TotalFreeSpace / (1024 * 1024 * 1024);

 }

    }

    return freeSpace;

}

以上是 C#实现获取磁盘空间大小的方法 的全部内容, 来源链接: utcz.com/z/358963.html

回到顶部