コード例 #1
0
        /// <summary>
        /// Accitional query over Win32_PnPEntity. This is needed to get data which is already connected to the machine.
        /// Sorting all entries by the VID_PID string and from Caption recieving the virtual port name.
        /// </summary>
        public void CheckCurrentConnectedDevice()
        {
            ManagementClass            dev        = new ManagementClass("Win32_PnPEntity");
            ManagementObjectCollection collection = dev.GetInstances();

            foreach (var item in collection)
            {
                if (item.GetPropertyValue("PNPDeviceID").ToString().Contains(VID_PID))
                {
                    var port = new string(item.Properties["Caption"]?.Value.ToString()
                                          .SkipWhile(x => x != '(').Skip(1).TakeWhile(x => x != ')').ToArray());
                    if (!avalableports.Contains(port))
                    {
                        avalableports.Add(port);
                    }
                }
            }
            collection.Dispose();
            ReadersListUpdated?.Invoke(this, avalableports.ToList());
        }
コード例 #2
0
        /// <summary>
        /// Handing when new Entity is created or deleted.
        /// Checking entier collection for our VID_PID, recieving from property Caption virtual port name
        /// checking if that port is already in our list (sometimes events can trigger multiple times, so we dont want to add or remove same entry)
        /// Checking if our operation is deleation or creation and based on the adding or removing from the list
        /// Over our event, notifing subscribers that list of avalable ports changed
        /// </summary>
        /// <param name="_event">event instance, to get instance of the ManagementObject</param>
        /// <param name="remove">device was added or removed</param>
        private void GetNewList(EventArrivedEventArgs _event, bool remove = false)
        {
            ManagementBaseObject instance = (ManagementBaseObject)_event.NewEvent["TargetInstance"];
            var port = new string(instance.Properties["Caption"]?.Value.ToString()
                                  .SkipWhile(x => x != '(').Skip(1).TakeWhile(x => x != ')').ToArray());
            var t = instance.Properties["PNPDeviceID"]?.Value.ToString();

            if (instance.Properties["PNPDeviceID"]?.Value.ToString().Contains(VID_PID) ?? false)
            {
                if (remove)
                {
                    if (avalableports.Contains(port))
                    {
                        avalableports.Remove(port);
                    }
                }
                else
                if (!avalableports.Contains(port))
                {
                    avalableports.Add(port);
                }
            }
            ReadersListUpdated?.Invoke(this, avalableports.ToList());
        }