Home > DeveloperSection > Articles > Check device connect to PC by c#

Check device connect to PC by c#


C# C# 
Ratings:
0 Comment(s)
 12473  View(s)
Rate this:

Check device connect to PC by c#

In this article I’m trying to make a small demo which is fetching all attached USB Devices from your computer.

Getting started: -

1.       Create a project in Visual Studio with appropriate name.

2.       Add a reference to System.Management in your project.

3.  Create a class with USBDeviceInfoClass name.

class USBDeviceInfoClass

{

   public USBDeviceInfoClass(string deviceID, string pnpDeviceID, string description)

   {

      this.DeviceID = deviceID;

      this.PnpDeviceID = pnpDeviceID;

      this.Description = description;

   }

   public string DeviceID { get; private set; }

   public string PnpDeviceID { get; private set; }

   public string Description { get; private set; }

}

 

4.  Create a method which is return the list of connected devices of computer GetConnectedUSBDevices()

static List<USBDeviceInfoClass> GetConnectedUSBDevices()

{

List<USBDeviceInfoClass> _devices = new List<USBDeviceInfoClass>(); // Create      List of USBDeviceInfoClass with _devices named

ManagementObjectCollection collection; // Create a object of ManagementObjectCollection class which is hold the list of connected devices     using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub

 "))// Fetch all devices with the help of WQL (Windows Query Language)

    {

        collection = searcher.Get();// save the list of connected devices with this statement to collection

    }

 

    foreach (var device in collection) // Set the DeviceID, PNPDeviceID and Description in _devices object properties.

    {

        _devices.Add(new USBDeviceInfoClass(

        (string)device.GetPropertyValue("DeviceID"),

        (string)device.GetPropertyValue("PNPDeviceID"),

        (string)device.GetPropertyValue("Description")

        ));

   

    }

 

    collection.Dispose();// dispose the collection object

    return _devices;

}

 

Note: - PNP stands for Plug and Play

Full Code: -

public partial class Form1 : Form

{

    public Form1()

    {

        InitializeComponent();

    }

    class USBDeviceInfoClass

    {

        public USBDeviceInfoClass(string deviceID, string pnpDeviceID, string   description)

        {

            this.DeviceID = deviceID;

            this.PnpDeviceID = pnpDeviceID;

            this.Description = description;

        }

        public string DeviceID { get; private set; }

        public string PnpDeviceID { get; private set; }

        public string Description { get; private set; }

     }

     private void Form1_Load(object sender, EventArgs e)

     {

        var usbDevices = GetConnectedUSBDevices();

        dgvDevices.DataSource = usbDevices;

        dgvDevices.Columns[0].Width = 233;

        dgvDevices.Columns[1].Width = 233;

        dgvDevices.Columns[2].Width = 101;

     }

     static List<USBDeviceInfoClass> GetConnectedUSBDevices()

     {

        List<USBDeviceInfoClass> _devices = new List<USBDeviceInfoClass>(); // Create List of USBDeviceInfoClass with _devices named

 

        ManagementObjectCollection collection; // Create a object of ManagementObjectCollection class which is hold the list of connected devices

        using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub")) // This line fetch all the connected device with windows query

        {

           collection = searcher.Get();

        }

 

       foreach (var device in collection)

       {

          _devices.Add(new USBDeviceInfoClass(

           (string)device.GetPropertyValue("DeviceID"),

           (string)device.GetPropertyValue("PNPDeviceID"),

           (string)device.GetPropertyValue("Description")

          ));

           

       }

 

       collection.Dispose();

       return _devices;

    }

}

 

Output: -

Check device connect to PC by c#

 


Don't want to miss updates? Please click the below button!

Follow MindStick