簡體   English   中英

識別USB flash驅動器在C#

[英]Identify USB flash drive in C#

我需要一個唯一標識 USB flash 驅動器的 ID。 該 ID 應該已經存在於 flash 驅動器上,因此它在我插入的每台 PC 上都是相同的 ID。看來 PNPDeviceID 不是唯一的。 誰能證明這一點? 如果它不是唯一的,是否有另一個 ID 來唯一標識 USB flash 驅動器?

我已經閱讀了這些答案,但它們無法幫助我解決問題:

PNPDeviceID格式

PNPDeviceID 是否唯一

c#.NET USB 設備持久標識符

編輯:

這是我為獲取 ID 而編寫的代碼。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Management;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApplication6
    {
        class USBDeviceInfo
        {
            public USBDeviceInfo(string deviceID, string pnpDeviceID, string description)
            {
                this.DeviceID = deviceID;
                this.PnpDeviceID = pnpDeviceID;
                this.Description = description;
            }
            public string DeviceID { get; private set; }
            public string PnpDeviceID { get; private set; }
            public string Description { get; private set; }
        }

        class Program
        {
            static List<string> Drives = new List<string>();

            static List<USBDeviceInfo> GetUSBDevices()
            {
                List<USBDeviceInfo> devices = new List<USBDeviceInfo>();

                ManagementObjectCollection collection;
                using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub"))
                    collection = searcher.Get();

                foreach (var device in collection)
                {
                    devices.Add(new USBDeviceInfo(
                    (string)device.GetPropertyValue("DeviceID"),
                    (string)device.GetPropertyValue("PNPDeviceID"),
                    (string)device.GetPropertyValue("Description")
                    ));
                }
                collection.Dispose();
                return devices;
            }

            static void Main(string[] args)
            {
                var usbDevices = GetUSBDevices();

                foreach (var usbDevice in usbDevices)
                {
                    // USB-Massenspeichergerät means USB mass storage device in english
                    if (usbDevice.Description.Equals("USB-Massenspeichergerät"))
                    {
                        Console.WriteLine(usbDevice.PnpDeviceID);           
                    }
                }
                Console.ReadKey();
            }
        }
    }

這是我從同一供應商和完全相同類型的兩個不同的 USB flash 驅動器獲得的一些示例 ID(無論我采用 DeviceID 還是 PNPDeviceID,它們都是相同的):

在此處輸入圖像描述

如您所見,VID 和 PID 是等效的。 它們之間的唯一區別是“\”后面的數字。 正如我在PNPDeviceID Format的答案中讀到的那樣,它似乎只是系統創建的設備實例,用於識別 flash 驅動器。 因此,在閱讀此答案后,我不確定那是否是唯一的。

這是一個英文版本的代碼。

代碼中已經提到了這一點。

我剛修好英語

請使用 nuget package 經理添加“System.Management”。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication6
{
    class USBDeviceInfo
    {
        public USBDeviceInfo(string deviceID, string pnpDeviceID, string description)
        {
            this.DeviceID = deviceID;
            this.PnpDeviceID = pnpDeviceID;
            this.Description = description;
        }
        public string DeviceID { get; private set; }
        public string PnpDeviceID { get; private set; }
        public string Description { get; private set; }
    }

    class Program
    {
        static List<string> Drives = new List<string>();

        static List<USBDeviceInfo> GetUSBDevices()
        {
            List<USBDeviceInfo> devices = new List<USBDeviceInfo>();

            ManagementObjectCollection collection;
            using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub"))
                collection = searcher.Get();

            foreach (var device in collection)
            {
                devices.Add(new USBDeviceInfo(
                (string)device.GetPropertyValue("DeviceID"),
                (string)device.GetPropertyValue("PNPDeviceID"),
                (string)device.GetPropertyValue("Description")
                ));
            }
            collection.Dispose();
            return devices;
        }

        static void Main(string[] args)
        {
            var usbDevices = GetUSBDevices();

            foreach (var usbDevice in usbDevices)
            {
                // USB-Massenspeichergerät means USB mass storage device in english
                if (usbDevice.Description.Equals("USB Mass Storage Device"))
                {
                    Console.WriteLine(usbDevice.PnpDeviceID);
                }
            }
            Console.ReadKey();
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM