Détecter un matériel connecté via son PID

//Classe permettant d"identifier une matériel connecté via son PID

 

using System;

using System.Management;

 

namespace ScanMateriel

{

     public class ScanMateriel

     {

          public bool resultat;

          public bool scaninprogress;

          public int resultat_taillescan;

          public String resultat_scan;

          private ConsoleKeyInfo keyscan;

     

          public ScanMateriel()

          {

               this.resultat = false;

               InitialiseScan();

          }

 

          public void InitialiseScan()

          {

                this.resultat_taillescan = 0;

                this.resultat_scan = "";

                this.scaninprogress = false;

          }

 

          public void GetUSBDevices(String PIDs)

          {

               String[] ListePID = PIDs.Split(";");

               this.resultat = false;

 

               if (ListePID.Length>0)

               {

                    foreach (string PID in ListePID)

                    {

                         if (PID == "") continue;

                         ManagementObjectCollection collection;

                        

                         using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_Keyboard Where DeviceID Like "" + PID + "%""))

                         {

                              collection = searcher.Get();

                         }

                        

                         this.resultat = collection.Count <= 0 ? false : true;

                         collection.Dispose();

 

                        if (this.resultat) break;

                    }

               }

          }

 

          public void WaitScan()

          {

               bool whilescan = true;

               this.scaninprogress = false;

 

               do

               {

                    this.keyscan = Console.ReadKey();

                   

                    if ((this.keyscan.KeyChar == "º") && (!this.scaninprogress))

                    {

                         this.resultat_taillescan = 0;

                         this.resultat_scan = "";

                         this.scaninprogress = true;

                    }

                    else if ((this.keyscan.KeyChar == "º") && (this.scaninprogress))

                    {

                         whilescan = false;

                    }

                    else if (this.keyscan.KeyChar != "º") //186

                    {

                         this.resultat_scan += this.keyscan.KeyChar;

                         this.resultat_taillescan++;

                    }

               }

              

               while (whilescan);

               this.scaninprogress = false;

          }

 

          public static void Main()

          {

               ScanMateriel t = new ScanMateriel();

               t.WaitScan();

               Console.WriteLine("Valeur : " + t.resultat_scan + " " + "Taille : " + t.resultat_taillescan);

               Console.ReadLine();

          }

     }

}