最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

方法一:利用System.IO.DriveInfo.GetDrives方法來獲取

復(fù)制代碼 代碼如下:
///  
/// 獲取指定驅(qū)動器的空間總大小(單位為B)
///  
///  只需輸入代表驅(qū)動器的字母即可 (大寫)
///   
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ū)動器的剩余空間總大小(單位為B)
///  
///  只需輸入代表驅(qū)動器的字母即可 
///   
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")來獲取
復(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
 {
     // 磁盤名稱
     diskInfo["Name"] =disk["Name"].ToString();
     // 磁盤描述
     diskInfo["Description"]=disk["Description"].ToString();
     // 磁盤總?cè)萘浚捎每臻g,已用空間
     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;
 }
}

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

相關(guān)文章

最新評論

昌宁县| 延寿县| 海阳市| 皋兰县| 扎兰屯市| 塔河县| 盱眙县| 乾安县| 兴宁市| 化州市| 平阳县| 吕梁市| 麻栗坡县| 韩城市| 云和县| 枝江市| 普陀区| 磐安县| 宁陵县| 遂宁市| 泽普县| 黑水县| 阿坝县| 崇阳县| 罗定市| 中宁县| 兴安县| 繁昌县| 循化| 福建省| 吴桥县| 疏附县| 鹤壁市| 上犹县| 和硕县| 通榆县| 瑞金市| 长岭县| 松潘县| 行唐县| 东乡族自治县|