• 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;
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    方法二:利用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;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
  • 相关阅读:
    Linux系统安全及应用(1)
    文件系统考古 3:1994 - The SGI XFS Filesystem
    电动变焦镜头的控制
    python单元测试
    vue实现响应式改变scss样式
    python裁剪图片
    springboot基于Java的电影院售票与管理系统毕业设计源码011449
    【过滤器设计模式详解】C/Java/JS/Go/Python/TS不同语言实现
    pnpm:简介
    Spark SQL案例【电商购买数据分析】
  • 原文地址:https://blog.csdn.net/weixin_44171249/article/details/133340249