zoukankan      html  css  js  c++  java
  • C#读取U盘序列号

    private List<string> _serialNumber = new List<string>();
    
    /// <summary>
    /// 调用这个函数将本机所有U盘序列号存储到_serialNumber中
    /// </summary>
    private void matchDriveLetterWithSerial()
    {
        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()));
                }
            }
        }
    }
    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("\"");
        var posFoundEnd = inValue.IndexOf("\"", posFoundStart + 1);
        var parsedValue = inValue.Substring(posFoundStart + 1, (posFoundEnd - posFoundStart) - 1);
        return parsedValue;
    }
  • 相关阅读:
    不错的英文商业站点,可以学习
    RegexMagic是好东东,可惜不会用
    PyBlosxom
    computer english
    已经在uubuntu下基本切换到chrome
    supervisor 管理后台进程
    精彩putty教程,乱码=,stdout,stderr重定向
    分析一个软件要考虑的问题
    Google has acquired AppJet Inc
    文件目录相关
  • 原文地址:https://www.cnblogs.com/goto/p/2442164.html
Copyright © 2011-2022 走看看