欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

C# 读取U盘序列号进行验证的示例代码

程序员文章站 2022-04-16 11:24:56
...
C# 读取U盘序列号进行验证的示例代码
private List<string> _serialNumber = new List<string>();

        public void matchDriveLetterWithSeral()
        {
            _serialNumber.Clear();
            string[] diskArray;
            string driveNumber;
            var searcher = new ManagementObjectSearcher("select * from Win32_LogicalDiskToPartition");
            foreach (ManagementObject dm in searcher.Get())
            {
                getValueInQuotes(dm["Dependent"].ToString());
                diskArray = getValueInQuotes(dm["Antecedent"].ToString()).Split(',');
                driveNumber = diskArray[0].Remove(0, 6).Trim();
                var disks = new ManagementObjectSearcher("select * from Win32_DiskDrive");
                foreach (ManagementObject disk in disks.Get())
                { 
                    if(disk["Name"].ToString()==("\\\\.\\PHYSICALDRIVE"+driveNumber)&&disk["InterfaceType"].ToString()=="USB")
                    {
                        _serialNumber.Add(parseSerialFromDeviceID(disk["PNPDeviceID"].ToString()));
                    }
                }
            }
        }

以上就是C# 读取U盘序列号进行验证的示例代码的详细内容,更多请关注其它相关文章!