C#getdrives类型固定但没有usb硬盘?

时间:2009-11-03 08:32:50

标签: c# usb hard-drive getdrivetype

我想检索系统中的固定磁盘列表。但是C#的GetDrives固定驱动器包括USB硬盘插头。

我知道如何检测到固定硬盘不是USB硬盘,反之亦然?

4 个答案:

答案 0 :(得分:4)

解决方案来自How to get serial number of USB-Stick in C#

 //import the System.Management namespace at the top in your "using" statement.
 ManagementObjectSearch theSearcher = new ManagementObjectSearcher(
      "SELECT * FROM Win32_DiskDrive WHERE InterfaceType='USB'");

答案 1 :(得分:3)

使用DriveType检测驱动器的类型:

using System.IO;

DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
  if (d.IsReady && d.DriveType == DriveType.Fixed)
  {
    // This is the drive you want...
  }
}

DriveInfo Class

EDIT1:

检查以下链接: How do I detected whether a hard drive is connected via USB?

答案 2 :(得分:1)

您可以在此处获取USB硬盘列表。

currentTime()

您可以在此处获取所有固定驱动器(系统和USB硬盘)的列表:

//Add Reference System.Management and use namespace at the top of the code.
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE InterfaceType='USB'");

        foreach (ManagementObject queryObj in searcher.Get())
        {
            foreach (ManagementObject b in queryObj.GetRelated("Win32_DiskPartition"))
            {
                foreach (ManagementBaseObject c in b.GetRelated("Win32_LogicalDisk"))
                { 
                    Console.WriteLine(String.Format("{0}" + "\\", c["Name"].ToString())); // here it will print USB drive letter
                }
            }

        }

如果你比较它们,那么你可以检索系统中没有USB硬盘的固定磁盘列表。

答案 3 :(得分:0)

将此MSDN链接用于各个解决方案(包括查找驱动器号): WMI Tasks: Disks and File Systems