U盘
private void ScanDisk()
{
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (var drive in drives)
{
// 可移动存储设备,且不是A盘
if ((drive.DriveType == DriveType.Removable) && false == drive.Name.Substring(0, 1).Equals("A"))
{
Console.WriteLine("找到一个U盘:" + drive.Name);
}
}
}
移动硬盘
public static List<string> GetListDisk() { List<string> lstDisk = new List<string>(); ManagementClass mgtCls = new ManagementClass("Win32_DiskDrive"); var disks = mgtCls.GetInstances(); foreach (ManagementObject mo in disks) { //if (mo.Properties["InterfaceType"].Value.ToString() != "SCSI" // && mo.Properties["InterfaceType"].Value.ToString() != "USB" // ) // continue; if (mo.Properties["MediaType"].Value == null || mo.Properties["MediaType"].Value.ToString() != "External hard disk media") { continue; } //foreach (var prop in mo.Properties) //{ // Console.WriteLine(prop.Name + "\t" + prop.Value); //} foreach (ManagementObject diskPartition in mo.GetRelated("Win32_DiskPartition")) { foreach (ManagementBaseObject disk in diskPartition.GetRelated("Win32_LogicalDisk")) { lstDisk.Add(disk.Properties["Name"].Value.ToString()); } } } return lstDisk; }
此代码是通过找 Win32_DiskDrive,Win32_DiskPartition,Win32_LogicalDisk 对应的属性值的规律, 三个之间的关系 得出 移动硬盘的盘符的。