亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C#實(shí)現(xiàn)獲取磁盤(pán)空間大小的方法

 更新時(shí)間:2014年12月19日 11:51:25   投稿:shichen2014  
這篇文章主要介紹了C#實(shí)現(xiàn)獲取磁盤(pán)空間大小的方法,分別基于System.IO.DriveInfo.GetDrives方法與ManagementClass("Win32_LogicalDisk")來(lái)實(shí)現(xiàn)這一功能,是非常實(shí)用的技巧,需要的朋友可以參考下

本文實(shí)例講述了C#實(shí)現(xiàn)獲取磁盤(pán)空間大小的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

方法一:利用System.IO.DriveInfo.GetDrives方法來(lái)獲取

復(fù)制代碼 代碼如下:
///  
/// 獲取指定驅(qū)動(dòng)器的空間總大小(單位為B)
///  
///  只需輸入代表驅(qū)動(dòng)器的字母即可 (大寫(xiě))
///   
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;
}

///  
/// 獲取指定驅(qū)動(dòng)器的剩余空間總大小(單位為B)
///  
///  只需輸入代表驅(qū)動(dòng)器的字母即可 
///   
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;
}


方法二:利用ManagementClass("Win32_LogicalDisk")來(lái)獲取
復(fù)制代碼 代碼如下:
List<Dictionary<string, string>> diskInfoDic = new List<Dictionary<string, string>>();
ManagementClass diskClass = new ManagementClass("Win32_LogicalDisk");
ManagementObjectCollection disks = diskClass.GetInstances();
foreach(ManagementObject disk in disks)
{
 Dictionary<string, string> diskInfo = new Dictionary<string, string>();
 try
 {
     // 磁盤(pán)名稱
     diskInfo["Name"] =disk["Name"].ToString();
     // 磁盤(pán)描述
     diskInfo["Description"]=disk["Description"].ToString();
     // 磁盤(pán)總?cè)萘?,可用空間,已用空間
     if (System.Convert.ToInt64(disk["Size"]) > 0)
     {
  long totalSpace = System.Convert.ToInt64(disk["Size"]) / MB;
  long freeSpace = System.Convert.ToInt64(disk["FreeSpace"]) / MB;
  long usedSpace = totalSpace - freeSpace;
       diskInfo["totalSpace"]=totalSpace.ToString();
  diskInfo["usedSpace"]=usedSpace.ToString();
  diskInfo["freeSpace"]=freeSpace.ToString();
     }
     diskInfoDic.Add(diskInfo);
 }
 catch(Exception ex)
 {
     Throw ex;
 }
}

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論