找到USB大容量存储设备“磁盘字母",然后单击“确定".通过其PID和VID号或名称 [英] Finding a USB mass storage devices "disk letter" by its PID and VID number or name

查看:157
本文介绍了找到USB大容量存储设备“磁盘字母",然后单击“确定".通过其PID和VID号或名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个该应用程序将显示USB大容量存储设备上的可用字节数.我的问题是设备磁盘号"(E:\ F:\…)可能会不时更改.我知道我的USB设备的PID和VID号,我知道设备的名称是什么.是否可以从此信息中找到设备字母,该怎么办?

.

谢谢

KimSchjodt 解决方案

 通过使用 System.Management ,您可以获得有关USB存储设备所需的所有信息,包括用于标识所需设备的设备的序列号,这段代码说明了如何获取有关所有设备的这些信息. >

 ManagementObjectSearcher diskDrives = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE InterfaceType ='USB'");
            foreach(diskDrives.Get()中的ManagementObject diskDrive)
            {
                字符串DeviceID = diskDrive ["DeviceID"].ToString();
                字符串DriveLetter =";
                字符串DriveDescription =";

                //将物理磁盘与分区关联
                ManagementObjectSearcher partitionSearcher =新的ManagementObjectSearcher(String.Format(
                    "{{Win32_DiskDrive.DeviceID ='{0}'}}的关联者,其中AssocClass = Win32_DiskDriveToDiskPartition",
                    diskDrive ["DeviceID"])));

                foreach(partitionSearcher.Get()中的ManagementObject分区)
                {
                    //将分区与逻辑磁盘(驱动器号卷)关联
                    ManagementObjectSearcher logicalSearcher =新的ManagementObjectSearcher(String.Format(
                        "{{Win32_DiskPartition.DeviceID ='{0}'}}的关联者,其中AssocClass = Win32_LogicalDiskToPartition",
                        partition ["DeviceID"])));

                    foreach(LogicalSearcher.Get()中的ManagementObject逻辑)
                    {
                        //最后找到逻辑磁盘条目以确定卷名
                        ManagementObjectSearcher volumeSearcher =新的ManagementObjectSearcher(String.Format(
                            "从Win32_LogicalDisk中选择*,其中Name ='{0}''',
                            逻辑[名称"])));

                        foreach(volumeSearcher.Get()中的ManagementObject卷)
                        {
                            DriveLetter = volume ["Name"].ToString();
                            if(volume [" VolumeName"]!= null)
                                DriveDescription = volume ["VolumeName"].ToString();

                            char VolumeLetter = DriveLetter [0];
                            字符串VolumeName = DriveDescription;
                            字符串制造商=(string)diskDrive [制造商"];
                            字符串MediaType =(string)diskDrive ["MediaType"];
                            string Model =(string)diskDrive ["Model"];
                            字符串SerialNumber =(string)diskDrive ["SerialNumber"];
                            long Size = Convert.ToInt64(volume ["Size"]);
                            long FreeSpace = Convert.ToInt64(volume ["FreeSpace"]));
                        }
                    }
                }
            } 

如果此答案有帮助,请记住通过将有用的帖子标记为答案来关闭话题

票价


Hi

I’m trying to write an application that will display how many bytes is free on a USB mass storage device. My problem is that the device "disk letter" (E:\ F:\ …) can change from time to time. I know my USB devices PID and VID number and I know what the device name is. Is it possible to find the device letter from this information and how can I do it?

 

Thanks

KimSchjodt    

 

解决方案

Hi,

 By using System.Management you can get all the information you need about the USB storage devices including the serial number of the device to identify the device you want, this piece of code shows how to get these information about all devices.

ManagementObjectSearcher diskDrives = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE InterfaceType='USB'");
            foreach (ManagementObject diskDrive in diskDrives.Get())
            {
                string DeviceID = diskDrive["DeviceID"].ToString();
                string DriveLetter = "";
                string DriveDescription = "";

                // associate physical disks with partitions
                ManagementObjectSearcher partitionSearcher = new ManagementObjectSearcher(String.Format(
                    "associators of {{Win32_DiskDrive.DeviceID='{0}'}} where AssocClass = Win32_DiskDriveToDiskPartition",
                    diskDrive["DeviceID"]));

                foreach (ManagementObject partition in partitionSearcher.Get())
                {
                    // associate partitions with logical disks (drive letter volumes)
                    ManagementObjectSearcher logicalSearcher = new ManagementObjectSearcher(String.Format(
                        "associators of {{Win32_DiskPartition.DeviceID='{0}'}} where AssocClass = Win32_LogicalDiskToPartition",
                        partition["DeviceID"]));

                    foreach (ManagementObject logical in logicalSearcher.Get())
                    {
                        // finally find the logical disk entry to determine the volume name
                        ManagementObjectSearcher volumeSearcher = new ManagementObjectSearcher(String.Format(
                            "select * from Win32_LogicalDisk where Name='{0}'",
                            logical["Name"]));

                        foreach (ManagementObject volume in volumeSearcher.Get())
                        {
                            DriveLetter = volume["Name"].ToString();
                            if (volume["VolumeName"] != null)
                                DriveDescription = volume["VolumeName"].ToString();

                            char VolumeLetter = DriveLetter[0];
                            string VolumeName = DriveDescription;
                            string Manufacturer = (string)diskDrive["Manufacturer"];
                            string MediaType = (string)diskDrive["MediaType"];
                            string Model = (string)diskDrive["Model"];
                            string SerialNumber = (string)diskDrive["SerialNumber"];
                            long Size = Convert.ToInt64(volume["Size"]);
                            long FreeSpace = Convert.ToInt64(volume["FreeSpace"]);
                        }
                    }
                }
            }

If this answer was helpful please remember to close your threads by marking helpful posts as answer

Fares


这篇关于找到USB大容量存储设备“磁盘字母",然后单击“确定".通过其PID和VID号或名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆