• 读取U盘信息的方法


    public class UsbDiskInfo
        {
            public string Display { get; set; }
            public string Value { get; set; }
        }
    
        public class UsbDiskHelper
        {
            //返回结果大致为:AAGP30PMDRVS56NK
            public static IEnumerable<UsbDiskInfo> GetUsbDisksInfo()
            {
                List<UsbDiskInfo> list = new List<UsbDiskInfo>();
                list.Add(new UsbDiskInfo{Display=DropDownListHelper.PLEASE_CHOOSE, Value=null});
                var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDiskToPartition");
                foreach (ManagementObject dm in searcher.Get())
                {
                    //dm["Dependent"] 大致为:\\JIANG-PC\root\cimv2:Win32_LogicalDisk.DeviceID="H:"
                    string strDriveLetter = GetValueInQuotes(dm["Dependent"].ToString());
    
                    //dm["Antecedent"] 大致为:\\JIANG-PC\root\cimv2:Win32_DiskPartition.DeviceID="Disk #1, Partition #0"
                    string[] diskArray = GetValueInQuotes(dm["Antecedent"].ToString()).Split(',');
                    string driveNumber = diskArray[0].Remove(0, 6).Trim();
                    var disks = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
                    foreach (ManagementObject disk in disks.Get())
                    {
                        //disk["Name"] 大致为:\\.\PHYSICALDRIVE1
                        //disk["InterfaceType"] 大致为:USB
                        if (disk["Name"].ToString() == ("\\\\.\\PHYSICALDRIVE" + driveNumber) && disk["InterfaceType"].ToString() == "USB")
                        {
                            //disk["PNPDeviceID"] 大致为:USBSTOR\DISK&VEN_TECLAST&PROD_COOLFLASH(S)&REV_1100\AAGP30PMDRVS56NK&0
                            list.Add(new UsbDiskInfo { Display = strDriveLetter, Value = ParseSerialFromDeviceId(disk["PNPDeviceID"].ToString()) });
                        }
                    }
                }
                return list;
            }
    
            private static string ParseSerialFromDeviceId(string deviceId)
            {
                var splitDeviceId = deviceId.Split('\\');
                var arrayLen = splitDeviceId.Length - 1;
                var serialArray = splitDeviceId[arrayLen].Split('&');
                var serial = serialArray[0];
                return serial;
            }
    
            private static string GetValueInQuotes(string inValue)
            {
                var posFoundStart = inValue.IndexOf("\"", StringComparison.Ordinal);
                var posFoundEnd = inValue.IndexOf("\"", posFoundStart + 1, StringComparison.Ordinal);
                var parsedValue = inValue.Substring(posFoundStart + 1, (posFoundEnd - posFoundStart) - 1);
                return parsedValue;
            }
  • 相关阅读:
    Vue less使用scope时渗入修改子组件样式
    Spring容器初始话原理图
    Java的动态代理
    Spring_xml和注解混合方式开发
    Spring_xml方式开发
    Spring入门初体验
    数论
    虚拟IP和IP漂移
    字符串hash + 二分答案
    字符串hash
  • 原文地址:https://www.cnblogs.com/zhangliangzlee/p/2921709.html
Copyright © 2020-2023  润新知