5

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

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

4

4 回答 4

4

解决方案来自如何在 C# 中获取 USB-Stick 的序列号

 //import the System.Management namespace at the top in your "using" statement.
 ManagementObjectSearch theSearcher = new ManagementObjectSearcher(
      "SELECT * FROM Win32_DiskDrive WHERE InterfaceType='USB'");
于 2009-11-03T09:04:31.863 回答
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 类

编辑1:

检查以下链接: 如何检测硬盘驱动器是否通过 USB 连接?

于 2009-11-03T08:40:51.350 回答
1

在这里你可以得到 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 硬盘)的列表:

        DriveInfo[] allDrives = DriveInfo.GetDrives(); 

        foreach (DriveInfo d in allDrives)
        {
            if (d.IsReady == true && d.DriveType == DriveType.Fixed)
            {
                Console.WriteLine("Drive {0}", d.Name);
                Console.WriteLine("  Drive type: {0}", d.DriveType);   
            }           
        }

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

于 2015-09-08T08:29:19.960 回答
0

将此 MSDN 链接用于单个解决方案(包括查找驱动器号): WMI 任务:磁盘和文件系统

于 2014-01-30T15:59:30.337 回答