zoukankan      html  css  js  c++  java
  • 读取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;
            }
  • 相关阅读:
    修复 XE8 for Android 方向传感器 headingX,Y,Z 不会动的问题
    修复 XE8 for Android 分享图片到 Gmail 权限不足的问题
    Firemonkey 载入 Style 皮肤 (*.fsf 二进制文件) 速度测试
    调整 FMX Android 文字显示「锯齿」效果
    [原创工具] ListView 调色盘 (Free)
    有关Linux的可执行程序
    Android 下配置一个 /dev/fb0 节点出来
    Android下运行Linux可执行程序
    数据库的范式
    rk3128 适配 USB 摄像头
  • 原文地址:https://www.cnblogs.com/zhangliangzlee/p/2921709.html
Copyright © 2011-2022 走看看