using System.Management;
using System.IO; //System.Management.dll
//创建WMI查询 ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from win32_logicaldisk"); ManagementObjectCollection moCollection = searcher.Get();//获取磁盘信息集合 foreach (ManagementObject disk in moCollection) { string DriveType = disk["DriveType"].ToString(); //disk["DriveType"]共有6中可能值,分别代表如下意义: //1:No type 2:Floppy disk 3:Hard disk //4:Removable drive or network drive 5:CD-ROM 6:RAM disk //本处只列出固定驱动器(硬盘分区)的情况 if (DriveType == "3") label1.Text += string.Format("Name='{0}' FileSystem='{1}' Size='{2}' FreeSpace='{3}' VolumeSerialNumber='{4}' ", disk["Name"].ToString(), disk["FileSystem"].ToString(), disk["Size"].ToString(),//info.TotalSize, //总大小 disk["FreeSpace"].ToString(),//info.TotalFreeSpace,//可用空闲空间量 disk.GetPropertyValue("VolumeSerialNumber").ToString()); } DriveInfo info = new DriveInfo("C");//创建对象 //输出磁盘容量和剩余空间 label1.Text += string.Format("磁盘格式{0} 可用空间大小为{3} bytes", info.DriveFormat, info.AvailableFreeSpace);