關於USB串口知識點c#代碼記載(定時掃描以及對應的USB插拔監聽分析等)

第一部分:

關於USB實時監聽事件:

using System;
using System.Management;
using System.Collections.Generic;

namespace Citms.Chrome.Browser
{
    public class ListenUSB
    {
        /// <summary>
        /// USB插入事件監視
        /// </summary>
        private ManagementEventWatcher insertWatcher = null;

        /// <summary>
        /// USB拔出事件監視
        /// </summary>
        private ManagementEventWatcher removeWatcher = null;

        /// <summary>
        /// 添加USB事件監視器
        /// </summary>
        /// <param name="usbInsertHandler">USB插入事件處理器</param>
        /// <param name="usbRemoveHandler">USB拔出事件處理器</param>
        /// <param name="withinInterval">發送通知允許的滯後時間</param>
        public Boolean AddUSBEventWatcher(EventArrivedEventHandler usbInsertHandler, EventArrivedEventHandler usbRemoveHandler, TimeSpan withinInterval)
        {
            try
            {
                ManagementScope Scope = new ManagementScope("root\\CIMV2");
                Scope.Options.EnablePrivileges = true;

                // USB插入監視
                if (usbInsertHandler != null)
                {
                    WqlEventQuery InsertQuery = new WqlEventQuery("__InstanceCreationEvent",
                        withinInterval,
                        "TargetInstance isa 'Win32_USBControllerDevice'");

                    insertWatcher = new ManagementEventWatcher(Scope, InsertQuery);
                    insertWatcher.EventArrived += usbInsertHandler;
                    insertWatcher.Start();
                }

                // USB拔出監視
                if (usbRemoveHandler != null)
                {
                    WqlEventQuery RemoveQuery = new WqlEventQuery("__InstanceDeletionEvent",
                        withinInterval,
                        "TargetInstance isa 'Win32_USBControllerDevice'");

                    removeWatcher = new ManagementEventWatcher(Scope, RemoveQuery);
                    removeWatcher.EventArrived += usbRemoveHandler;
                    removeWatcher.Start();
                }

                return true;
            }

            catch (Exception)
            {
                RemoveUSBEventWatcher();
                return false;
            }
        }

        /// <summary>
        /// 移去USB事件監視器
        /// </summary>
        public void RemoveUSBEventWatcher()
        {
            if (insertWatcher != null)
            {
                insertWatcher.Stop();
                insertWatcher = null;
            }

            if (removeWatcher != null)
            {
                removeWatcher.Stop();
                removeWatcher = null;
            }
        }

        /// <summary>
        /// USB控制設備類型
        /// </summary>
        public struct USBControllerDevice
        {
            /// <summary>
            /// USB控制器設備ID
            /// </summary>
            public String Antecedent;

            /// <summary>
            /// USB即插即用設備ID
            /// </summary>
            public String Dependent;
        }

        /// <summary>
        /// 定位發生插拔的USB設備
        /// </summary>
        /// <param name="e">USB插拔事件參數</param>
        /// <returns>發生插拔現象的USB控制設備ID</returns>
        public static List<USBControllerDevice> WhoUSBControllerDevice(EventArrivedEventArgs e)
        {
            ManagementBaseObject mbo = e.NewEvent["TargetInstance"] as ManagementBaseObject;
            if (mbo != null && mbo.ClassPath.ClassName == "Win32_USBControllerDevice")
            {
                String Antecedent = (mbo["Antecedent"] as String).Replace("\"", String.Empty).Split(new Char[] { '=' })[1];
                String Dependent = (mbo["Dependent"] as String).Replace("\"", String.Empty).Split(new Char[] { '=' })[1];
                return new List<USBControllerDevice>(1) { new USBControllerDevice { Antecedent = Antecedent, Dependent = Dependent } };
            }

            return null;
        }
    }
}

第二部分:

關於USB信息掃描獲取:

using System;
using System.Management;
using System.Text.RegularExpressions;
using System.Collections.Generic;

namespace Citms.Chrome.Browser
{
    /// <summary>
    /// 即插即用設備信息結構
    /// </summary>
    public struct PnPEntityInfo
    {
        public String PNPDeviceID;      // 設備ID
        public String Name;             // 設備名稱
        public String Description;      // 設備描述
        public String Service;          // 服務
        public String Status;           // 設備狀態
        public UInt16 VendorID;         // 供應商標識
        public UInt16 ProductID;        // 產品編號 
        public Guid ClassGuid;          // 設備安裝類GUID
    }

    /// <summary>
    /// 基於WMI獲取USB設備信息
    /// </summary>
    public partial class USBInfo
    {
        #region UsbDevice
        /// <summary>
        /// 獲取所有的USB設備實體(過濾沒有VID和PID的設備)
        /// </summary>
        public static PnPEntityInfo[] AllUsbDevices
        {
            get
            {
                return WhoUsbDevice(UInt16.MinValue, UInt16.MinValue, Guid.Empty);
            }
        }

        /// <summary>
        /// 查詢USB設備實體(設備要求有VID和PID)
        /// </summary>
        /// <param name="VendorID">供應商標識,MinValue忽視</param>
        /// <param name="ProductID">產品編號,MinValue忽視</param>
        /// <param name="ClassGuid">設備安裝類Guid,Empty忽視</param>
        /// <returns>設備列表</returns>
        public static PnPEntityInfo[] WhoUsbDevice(UInt16 VendorID, UInt16 ProductID, Guid ClassGuid)
        {
            List<PnPEntityInfo> UsbDevices = new List<PnPEntityInfo>();

            // 獲取USB控制器及其相關聯的設備實體
            ManagementObjectCollection USBControllerDeviceCollection = new ManagementObjectSearcher("SELECT * FROM Win32_USBControllerDevice").Get();
            if (USBControllerDeviceCollection != null)
            {
                foreach (ManagementObject USBControllerDevice in USBControllerDeviceCollection)
                {   // 獲取設備實體的DeviceID
                    String Dependent = (USBControllerDevice["Dependent"] as String).Split(new Char[] { '=' })[1];

                    // 過濾掉沒有VID和PID的USB設備
                    Match match = Regex.Match(Dependent, "VID_[0-9|A-F]{4}&PID_[0-9|A-F]{4}");
                    if (match.Success)
                    {
                        UInt16 theVendorID = Convert.ToUInt16(match.Value.Substring(4, 4), 16);   // 供應商標識
                        if (VendorID != UInt16.MinValue && VendorID != theVendorID) continue;

                        UInt16 theProductID = Convert.ToUInt16(match.Value.Substring(13, 4), 16); // 產品編號
                        if (ProductID != UInt16.MinValue && ProductID != theProductID) continue;

                        ManagementObjectCollection PnPEntityCollection = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE DeviceID=" + Dependent).Get();
                        if (PnPEntityCollection != null)
                        {
                            foreach (ManagementObject Entity in PnPEntityCollection)
                            {
                                Guid theClassGuid = new Guid(Entity["ClassGuid"] as String);    // 設備安裝類GUID
                                if (ClassGuid != Guid.Empty && ClassGuid != theClassGuid) continue;

                                PnPEntityInfo Element;
                                Element.PNPDeviceID = Entity["PNPDeviceID"] as String;  // 設備ID
                                Element.Name = Entity["Name"] as String;                // 設備名稱
                                Element.Description = Entity["Description"] as String;  // 設備描述
                                Element.Service = Entity["Service"] as String;          // 服務
                                Element.Status = Entity["Status"] as String;            // 設備狀態
                                Element.VendorID = theVendorID;     // 供應商標識
                                Element.ProductID = theProductID;   // 產品編號
                                Element.ClassGuid = theClassGuid;   // 設備安裝類GUID

                                UsbDevices.Add(Element);
                            }
                        }
                    }
                }
            }

            if (UsbDevices.Count == 0) return null; else return UsbDevices.ToArray();
        }

        /// <summary>
        /// 查詢USB設備實體(設備要求有VID和PID)
        /// </summary>
        /// <param name="VendorID">供應商標識,MinValue忽視</param>
        /// <param name="ProductID">產品編號,MinValue忽視</param>
        /// <returns>設備列表</returns>
        public static PnPEntityInfo[] WhoUsbDevice(UInt16 VendorID, UInt16 ProductID)
        {
            return WhoUsbDevice(VendorID, ProductID, Guid.Empty);
        }

        /// <summary>
        /// 查詢USB設備實體(設備要求有VID和PID)
        /// </summary>
        /// <param name="ClassGuid">設備安裝類Guid,Empty忽視</param>
        /// <returns>設備列表</returns>
        public static PnPEntityInfo[] WhoUsbDevice(Guid ClassGuid)
        {
            return WhoUsbDevice(UInt16.MinValue, UInt16.MinValue, ClassGuid);
        }

        /// <summary>
        /// 查詢USB設備實體(設備要求有VID和PID)
        /// </summary>
        /// <param name="PNPDeviceID">設備ID,可以是不完整信息</param>
        /// <returns>設備列表</returns>        
        public static PnPEntityInfo[] WhoUsbDevice(String PNPDeviceID)
        {
            List<PnPEntityInfo> UsbDevices = new List<PnPEntityInfo>();

            // 獲取USB控制器及其相關聯的設備實體
            ManagementObjectCollection USBControllerDeviceCollection = new ManagementObjectSearcher("SELECT * FROM Win32_USBControllerDevice").Get();
            if (USBControllerDeviceCollection != null)
            {
                foreach (ManagementObject USBControllerDevice in USBControllerDeviceCollection)
                {   // 獲取設備實體的DeviceID
                    String Dependent = (USBControllerDevice["Dependent"] as String).Split(new Char[] { '=' })[1];
                    if (!String.IsNullOrEmpty(PNPDeviceID))
                    {   // 注意:忽視大小寫
                        if (Dependent.IndexOf(PNPDeviceID, 1, PNPDeviceID.Length - 2, StringComparison.OrdinalIgnoreCase) == -1) continue;
                    }

                    // 過濾掉沒有VID和PID的USB設備
                    Match match = Regex.Match(Dependent, "VID_[0-9|A-F]{4}&PID_[0-9|A-F]{4}");
                    if (match.Success)
                    {
                        ManagementObjectCollection PnPEntityCollection = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE DeviceID=" + Dependent).Get();
                        if (PnPEntityCollection != null)
                        {
                            foreach (ManagementObject Entity in PnPEntityCollection)
                            {
                                PnPEntityInfo Element;
                                Element.PNPDeviceID = Entity["PNPDeviceID"] as String;  // 設備ID
                                Element.Name = Entity["Name"] as String;                // 設備名稱
                                Element.Description = Entity["Description"] as String;  // 設備描述
                                Element.Service = Entity["Service"] as String;          // 服務
                                Element.Status = Entity["Status"] as String;            // 設備狀態
                                Element.VendorID = Convert.ToUInt16(match.Value.Substring(4, 4), 16);   // 供應商標識   
                                Element.ProductID = Convert.ToUInt16(match.Value.Substring(13, 4), 16); // 產品編號                         // 產品編號
                                Element.ClassGuid = new Guid(Entity["ClassGuid"] as String);            // 設備安裝類GUID

                                UsbDevices.Add(Element);
                            }
                        }
                    }
                }
            }

            if (UsbDevices.Count == 0) return null; else return UsbDevices.ToArray();
        }

        /// <summary>
        /// 根據服務定位USB設備
        /// </summary>
        /// <param name="ServiceCollection">要查詢的服務集合</param>
        /// <returns>設備列表</returns>
        public static PnPEntityInfo[] WhoUsbDevice(String[] ServiceCollection)
        {
            if (ServiceCollection == null || ServiceCollection.Length == 0)
                return WhoUsbDevice(UInt16.MinValue, UInt16.MinValue, Guid.Empty);

            List<PnPEntityInfo> UsbDevices = new List<PnPEntityInfo>();

            // 獲取USB控制器及其相關聯的設備實體
            ManagementObjectCollection USBControllerDeviceCollection = new ManagementObjectSearcher("SELECT * FROM Win32_USBControllerDevice").Get();
            if (USBControllerDeviceCollection != null)
            {
                foreach (ManagementObject USBControllerDevice in USBControllerDeviceCollection)
                {   // 獲取設備實體的DeviceID
                    String Dependent = (USBControllerDevice["Dependent"] as String).Split(new Char[] { '=' })[1];

                    // 過濾掉沒有VID和PID的USB設備
                    Match match = Regex.Match(Dependent, "VID_[0-9|A-F]{4}&PID_[0-9|A-F]{4}");
                    if (match.Success)
                    {
                        ManagementObjectCollection PnPEntityCollection = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE DeviceID=" + Dependent).Get();
                        if (PnPEntityCollection != null)
                        {
                            foreach (ManagementObject Entity in PnPEntityCollection)
                            {
                                String theService = Entity["Service"] as String;          // 服務
                                if (String.IsNullOrEmpty(theService)) continue;

                                foreach (String Service in ServiceCollection)
                                {   // 注意:忽視大小寫
                                    if (String.Compare(theService, Service, true) != 0) continue;

                                    PnPEntityInfo Element;
                                    Element.PNPDeviceID = Entity["PNPDeviceID"] as String;  // 設備ID
                                    Element.Name = Entity["Name"] as String;                // 設備名稱
                                    Element.Description = Entity["Description"] as String;  // 設備描述
                                    Element.Service = theService;                           // 服務
                                    Element.Status = Entity["Status"] as String;            // 設備狀態
                                    Element.VendorID = Convert.ToUInt16(match.Value.Substring(4, 4), 16);   // 供應商標識   
                                    Element.ProductID = Convert.ToUInt16(match.Value.Substring(13, 4), 16); // 產品編號
                                    Element.ClassGuid = new Guid(Entity["ClassGuid"] as String);            // 設備安裝類GUID

                                    UsbDevices.Add(Element);
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            if (UsbDevices.Count == 0) return null; else return UsbDevices.ToArray();
        }
        #endregion

        #region PnPEntity
        /// <summary>
        /// 所有即插即用設備實體(過濾沒有VID和PID的設備)
        /// </summary>
        public static PnPEntityInfo[] AllPnPEntities
        {
            get
            {
                return WhoPnPEntity(UInt16.MinValue, UInt16.MinValue, Guid.Empty);
            }
        }

        /// <summary>
        /// 根據VID和PID及設備安裝類GUID定位即插即用設備實體
        /// </summary>
        /// <param name="VendorID">供應商標識,MinValue忽視</param>
        /// <param name="ProductID">產品編號,MinValue忽視</param>
        /// <param name="ClassGuid">設備安裝類Guid,Empty忽視</param>
        /// <returns>設備列表</returns>
        /// <remarks>
        /// HID:{745a17a0-74d3-11d0-b6fe-00a0c90f57da}
        /// Imaging Device:{6bdd1fc6-810f-11d0-bec7-08002be2092f}
        /// Keyboard:{4d36e96b-e325-11ce-bfc1-08002be10318} 
        /// Mouse:{4d36e96f-e325-11ce-bfc1-08002be10318}
        /// Network Adapter:{4d36e972-e325-11ce-bfc1-08002be10318}
        /// USB:{36fc9e60-c465-11cf-8056-444553540000}
        /// </remarks>
        public static PnPEntityInfo[] WhoPnPEntity(UInt16 VendorID, UInt16 ProductID, Guid ClassGuid)
        {
            List<PnPEntityInfo> PnPEntities = new List<PnPEntityInfo>();

            // 枚舉即插即用設備實體
            String VIDPID;
            if (VendorID == UInt16.MinValue)
            {
                if (ProductID == UInt16.MinValue)
                    VIDPID = "'%VID[_]____&PID[_]____%'";
                else
                    VIDPID = "'%VID[_]____&PID[_]" + ProductID.ToString("X4") + "%'";
            }
            else
            {
                if (ProductID == UInt16.MinValue)
                    VIDPID = "'%VID[_]" + VendorID.ToString("X4") + "&PID[_]____%'";
                else
                    VIDPID = "'%VID[_]" + VendorID.ToString("X4") + "&PID[_]" + ProductID.ToString("X4") + "%'";
            }

            String QueryString;
            if (ClassGuid == Guid.Empty)
                QueryString = "SELECT * FROM Win32_PnPEntity WHERE PNPDeviceID LIKE" + VIDPID;
            else
                QueryString = "SELECT * FROM Win32_PnPEntity WHERE PNPDeviceID LIKE" + VIDPID + " AND ClassGuid='" + ClassGuid.ToString("B") + "'";

            ManagementObjectCollection PnPEntityCollection = new ManagementObjectSearcher(QueryString).Get();
            if (PnPEntityCollection != null)
            {
                foreach (ManagementObject Entity in PnPEntityCollection)
                {
                    String PNPDeviceID = Entity["PNPDeviceID"] as String;
                    Match match = Regex.Match(PNPDeviceID, "VID_[0-9|A-F]{4}&PID_[0-9|A-F]{4}");
                    if (match.Success)
                    {
                        PnPEntityInfo Element;

                        Element.PNPDeviceID = PNPDeviceID;                      // 設備ID
                        Element.Name = Entity["Name"] as String;                // 設備名稱
                        Element.Description = Entity["Description"] as String;  // 設備描述
                        Element.Service = Entity["Service"] as String;          // 服務
                        Element.Status = Entity["Status"] as String;            // 設備狀態
                        Element.VendorID = Convert.ToUInt16(match.Value.Substring(4, 4), 16);   // 供應商標識
                        Element.ProductID = Convert.ToUInt16(match.Value.Substring(13, 4), 16); // 產品編號
                        Element.ClassGuid = new Guid(Entity["ClassGuid"] as String);            // 設備安裝類GUID

                        PnPEntities.Add(Element);
                    }
                }
            }

            if (PnPEntities.Count == 0) return null; else return PnPEntities.ToArray();
        }

        /// <summary>
        /// 根據VID和PID定位即插即用設備實體
        /// </summary>
        /// <param name="VendorID">供應商標識,MinValue忽視</param>
        /// <param name="ProductID">產品編號,MinValue忽視</param>
        /// <returns>設備列表</returns>
        public static PnPEntityInfo[] WhoPnPEntity(UInt16 VendorID, UInt16 ProductID)
        {
            return WhoPnPEntity(VendorID, ProductID, Guid.Empty);
        }

        /// <summary>
        /// 根據設備安裝類GUID定位即插即用設備實體
        /// </summary>
        /// <param name="ClassGuid">設備安裝類Guid,Empty忽視</param>
        /// <returns>設備列表</returns>
        public static PnPEntityInfo[] WhoPnPEntity(Guid ClassGuid)
        {
            return WhoPnPEntity(UInt16.MinValue, UInt16.MinValue, ClassGuid);
        }

        /// <summary>
        /// 根據設備ID定位設備
        /// </summary>
        /// <param name="PNPDeviceID">設備ID,可以是不完整信息</param>
        /// <returns>設備列表</returns>
        /// <remarks>
        /// 注意:對於下劃線,需要寫成“[_]”,否則視爲任意字符
        /// </remarks>
        public static PnPEntityInfo[] WhoPnPEntity(String PNPDeviceID)
        {
            List<PnPEntityInfo> PnPEntities = new List<PnPEntityInfo>();

            // 枚舉即插即用設備實體
            String QueryString;
            if (String.IsNullOrEmpty(PNPDeviceID))
            {
                QueryString = "SELECT * FROM Win32_PnPEntity WHERE PNPDeviceID LIKE '%VID[_]____&PID[_]____%'";
            }
            else
            {   // LIKE子句中有反斜槓字符將會引發WQL查詢異常
                QueryString = "SELECT * FROM Win32_PnPEntity WHERE PNPDeviceID LIKE '%" + PNPDeviceID.Replace('\\', '_') + "%'";
            }

            ManagementObjectCollection PnPEntityCollection = new ManagementObjectSearcher(QueryString).Get();
            if (PnPEntityCollection != null)
            {
                foreach (ManagementObject Entity in PnPEntityCollection)
                {
                    String thePNPDeviceID = Entity["PNPDeviceID"] as String;
                    Match match = Regex.Match(thePNPDeviceID, "VID_[0-9|A-F]{4}&PID_[0-9|A-F]{4}");
                    if (match.Success)
                    {
                        PnPEntityInfo Element;

                        Element.PNPDeviceID = thePNPDeviceID;                   // 設備ID
                        Element.Name = Entity["Name"] as String;                // 設備名稱
                        Element.Description = Entity["Description"] as String;  // 設備描述
                        Element.Service = Entity["Service"] as String;          // 服務
                        Element.Status = Entity["Status"] as String;            // 設備狀態
                        Element.VendorID = Convert.ToUInt16(match.Value.Substring(4, 4), 16);   // 供應商標識
                        Element.ProductID = Convert.ToUInt16(match.Value.Substring(13, 4), 16); // 產品編號
                        Element.ClassGuid = new Guid(Entity["ClassGuid"] as String);            // 設備安裝類GUID

                        PnPEntities.Add(Element);
                    }
                }
            }

            if (PnPEntities.Count == 0) return null; else return PnPEntities.ToArray();
        }

        /// <summary>
        /// 根據服務定位設備
        /// </summary>
        /// <param name="ServiceCollection">要查詢的服務集合,null忽視</param>
        /// <returns>設備列表</returns>
        /// <remarks>
        /// 跟服務相關的類:
        ///     Win32_SystemDriverPNPEntity
        ///     Win32_SystemDriver
        /// </remarks>
        public static PnPEntityInfo[] WhoPnPEntity(String[] ServiceCollection)
        {
            if (ServiceCollection == null || ServiceCollection.Length == 0)
                return WhoPnPEntity(UInt16.MinValue, UInt16.MinValue, Guid.Empty);

            List<PnPEntityInfo> PnPEntities = new List<PnPEntityInfo>();

            // 枚舉即插即用設備實體
            String QueryString = "SELECT * FROM Win32_PnPEntity WHERE PNPDeviceID LIKE '%VID[_]____&PID[_]____%'";
            ManagementObjectCollection PnPEntityCollection = new ManagementObjectSearcher(QueryString).Get();
            if (PnPEntityCollection != null)
            {
                foreach (ManagementObject Entity in PnPEntityCollection)
                {
                    String PNPDeviceID = Entity["PNPDeviceID"] as String;
                    Match match = Regex.Match(PNPDeviceID, "VID_[0-9|A-F]{4}&PID_[0-9|A-F]{4}");
                    if (match.Success)
                    {
                        String theService = Entity["Service"] as String;            // 服務
                        if (String.IsNullOrEmpty(theService)) continue;

                        foreach (String Service in ServiceCollection)
                        {   // 注意:忽視大小寫
                            if (String.Compare(theService, Service, true) != 0) continue;

                            PnPEntityInfo Element;

                            Element.PNPDeviceID = PNPDeviceID;                      // 設備ID
                            Element.Name = Entity["Name"] as String;                // 設備名稱
                            Element.Description = Entity["Description"] as String;  // 設備描述
                            Element.Service = theService;                           // 服務
                            Element.Status = Entity["Status"] as String;            // 設備狀態
                            Element.VendorID = Convert.ToUInt16(match.Value.Substring(4, 4), 16);   // 供應商標識
                            Element.ProductID = Convert.ToUInt16(match.Value.Substring(13, 4), 16); // 產品編號
                            Element.ClassGuid = new Guid(Entity["ClassGuid"] as String);            // 設備安裝類GUID

                            PnPEntities.Add(Element);
                            break;
                        }
                    }
                }
            }

            if (PnPEntities.Count == 0) return null; else return PnPEntities.ToArray();
        }
        #endregion        
    }
}

第三部分:

實時開啓監聽,對應分析設備類型判斷給出處理方法:

  public bool Load_ListenUSB()
        {
            Static = USB.AddUSBEventWatcher(USBEventHandler, USBEventHandler, new TimeSpan(0, 0, 3));
            PnPEntityInfo[] USBST1= USBInfo.AllUsbDevices;
            List<bool> AssestStatuelist = new List<bool>();
            foreach (var item in USBST1)
            {
                bool AssestStatue= item.Name.Contains("U.are.U® 4500 Fingerprint Reader");
                AssestStatuelist.Add(AssestStatue);
            }
            if (AssestStatuelist.Contains(true))
            {

            }
            else
            {
                MessageBox.Show("請插入指紋儀USB,重啓軟件!");
            }          
            return Static;
        }

        private void USBEventHandler(Object sender, EventArrivedEventArgs e)
        {
            List<bool> AssestStatuelistS = new List<bool>();
            if (e.NewEvent.ClassPath.ClassName == "__InstanceCreationEvent")
            {
                //PnPEntityInfo[] USBST2 = USBInfo.AllUsbDevices;
                //foreach (var item in USBST2)
                //{
                //    bool AssestStatue = item.Name.Contains("U.are.U® 4500 Fingerprint Reader");
                //    AssestStatuelistS.Add(AssestStatue);
                //}
                //if (AssestStatuelistS.Contains(true))
                //{
                //    MessageBox.Show("再次插入指紋儀USB,重啓軟件生效!");
                //}
                //else
                //{
                   
                //}

            }
            else if (e.NewEvent.ClassPath.ClassName == "__InstanceDeletionEvent")
            {
                PnPEntityInfo[] USBST2 = USBInfo.AllUsbDevices;
                foreach (var item in USBST2)
                {
                    bool AssestStatue = item.Name.Contains("U.are.U® 4500 Fingerprint Reader");
                    AssestStatuelistS.Add(AssestStatue);                                                              
                }
                if (AssestStatuelistS.Contains(true))
                {

                }
                else
                {
                    MessageBox.Show("指紋儀外殼已拔出,請插入指紋儀重啓軟件!");
                }


            }
        }

這個位子在Load_ListenUSB方法體中開啓了USB監聽從而在USBEventHandler在中判斷設備信息確定是否爲同一型號設備處理。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章