Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如何找到USB驱动器号?_.net_Usb - Fatal编程技术网

.net 如何找到USB驱动器号?

.net 如何找到USB驱动器号?,.net,usb,.net,Usb,我正在编写一个安装程序,将应用程序安装到USB驱动器上。该应用程序只能从USB驱动器使用,因此自动选择要安装的USB驱动器将为用户节省额外的步骤 我可能会探索使用Nullsoft或MSI进行安装,但由于我对.NET比较熟悉,我最初计划尝试使用自定义.NET安装程序或.NET上的安装组件 是否可以使用.NET确定Windows上USB闪存驱动器的驱动器号?如何使用?您可以使用: from driveInfo in DriveInfo.GetDrives() where driveInfo.Driv

我正在编写一个安装程序,将应用程序安装到USB驱动器上。该应用程序只能从USB驱动器使用,因此自动选择要安装的USB驱动器将为用户节省额外的步骤

我可能会探索使用Nullsoft或MSI进行安装,但由于我对.NET比较熟悉,我最初计划尝试使用自定义.NET安装程序或.NET上的安装组件

是否可以使用.NET确定Windows上USB闪存驱动器的驱动器号?如何使用?

您可以使用:

from driveInfo in DriveInfo.GetDrives()
where driveInfo.DriveType == DriveType.Removable && driveInfo.IsReady
select driveInfo.RootDirectory.FullName

这将枚举系统上没有LINQ但仍使用WMI的所有驱动器:

// browse all USB WMI physical disks

foreach(ManagementObject drive in new ManagementObjectSearcher(
    "select * from Win32_DiskDrive where InterfaceType='USB'").Get())
{
    // associate physical disks with partitions

    foreach(ManagementObject partition in new ManagementObjectSearcher(
        "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" + drive["DeviceID"]
          + "'} WHERE AssocClass = 
                Win32_DiskDriveToDiskPartition").Get())
    {
        Console.WriteLine("Partition=" + partition["Name"]);

        // associate partitions with logical disks (drive letter volumes)

        foreach(ManagementObject disk in new ManagementObjectSearcher(
            "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='"
              + partition["DeviceID"]
              + "'} WHERE AssocClass =
                Win32_LogicalDiskToPartition").Get())
        {
            Console.WriteLine("Disk=" + disk["Name"]);
        }
    }

    // this may display nothing if the physical disk

    // does not have a hardware serial number

    Console.WriteLine("Serial="
     + new ManagementObject("Win32_PhysicalMedia.Tag='"
     + drive["DeviceID"] + "'")["SerialNumber"]);
}

C#2.0版的肯特代码(从我的头顶,未经测试):

IList fullNames=new List();
foreach(DriveInfo.GetDrives()中的DriveInfo-DriveInfo){
如果(driveInfo.DriveType==DriveType.Removable){
添加(driveInfo.RootDirectory.FullName);
}
}

程序本身是否从闪存驱动器运行?或者你只是想说“嘿,这是系统上的驱动器,这是哪些是闪存驱动器”?也有FireWire驱动器,但这实际上是正确的做法。但不返回USB硬盘驱动器,我认为这是一个问题。USB硬盘驱动器不完全是“USB闪存驱动器”,还是?;-)ManagementObject、ManagementObjectSearcher和ManagementObjectCollection(使用.Get()隐式检索)都有一个应调用的Dispose方法。我使用您的解决方案查找一个闪存驱动器的驱动器号,我知道该闪存驱动器的序列号。它在Windows 10下运行良好,但在Windows 7中序列号为空。我发现序列号也是驱动器[“PNPDeviceID”]的一部分,我现在测试它是否包含我要查找的序列号。谢谢
IList<String> fullNames = new List<String>();
foreach (DriveInfo driveInfo in DriveInfo.GetDrives()) {
    if (driveInfo.DriveType == DriveType.Removable) {
        fullNames.Add(driveInfo.RootDirectory.FullName);
    }
}