zoukankan      html  css  js  c++  java
  • 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; 
            }
    

    方法二:利用ManagementClass("Win32_LogicalDisk")来获取

    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();
    					// 磁盘总容量,可用空间,已用空间
    					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;
    				}
    			}
    

    更多IT相关资讯与技术文章,欢迎光临我的个人网站:http://www.zuowenjun.cn/

  • 相关阅读:
    深入理解java垃圾回收算法
    JVM类加载机制与对象的生命周期
    JVM 类加载机制详解
    从经典面试题看java中类的加载机制
    Intellij IDEA常用快捷键介绍 Intellij IDEA快捷键大全汇总
    Java HashMap 如何正确遍历并删除元素
    记录Java的垃圾回收机制和几种引用
    浅谈jvm中的垃圾回收策略
    Mysql常见四种索引的使用
    Java虚拟机垃圾回收(三) 7种垃圾收集器
  • 原文地址:https://www.cnblogs.com/zuowj/p/4173457.html
Copyright © 2011-2022 走看看