C# 获取本地电脑所有的盘符
C# 获取本地电脑所有的盘符
话不多说,直接上菜:
public List<string> GetRemovableDeviceID()
{
List<string> deviceIDs = new List<string>();
ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * From Win32_LogicalDisk ");
ManagementObjectCollection queryCollection = query.Get();
foreach (ManagementObject mo in queryCollection)
{
switch (int.Parse(mo["DriveType"].ToString()))
{
case (int)DriveType.Removable: //可以移动磁盘
{
//MessageBox.Show("可以移动磁盘");
deviceIDs.Add(mo["DeviceID"].ToString());
break;
}
case (int)DriveType.Fixed: //本地磁盘
{
//MessageBox.Show("本地磁盘");
deviceIDs.Add(mo["DeviceID"].ToString());
break;
}
case (int)DriveType.CDRom: //CD rom drives
{
//MessageBox.Show("CD rom drives ");
break;
}
case (int)DriveType.Network: //网络驱动
{
//MessageBox.Show("网络驱动器 ");
break;
}
case (int)DriveType.Ram:
{
//MessageBox.Show("驱动器是一个 RAM 磁盘 ");
break;
}
case (int)DriveType.NoRootDirectory:
{
//MessageBox.Show("驱动器没有根目录 ");
break;
}
default: //defalut to folder
{
//MessageBox.Show("驱动器类型未知 ");
break;
}
}
}
return deviceIDs;
}