---
title: "Check device connect to PC by c#"  
description: "In this article I’m trying to make a small demo which is fetching all attached USB Devices from your computer."  
author: "Vijay Shukla"  
published: 2013-10-10  
updated: 2020-03-27  
canonical: https://www.mindstick.com/articles/1376/check-device-connect-to-pc-by-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 3 minutes  

---

# Check device connect to PC by c#

## 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](https://www.mindstick.com/articles/996/select-all-checkbox-from-header-checkbox).\

Getting started: -

- 1. Create a [project in Visual](https://www.mindstick.com/forum/537/how-to-add-a-custom-icon-to-setup-project-in-visual-studio-2010) Studio with an appropriate name.
- 2. **Add** a [reference](https://www.mindstick.com/forum/34619/call-by-value-and-call-by-reference) to **System.Management** in [your project](https://www.mindstick.com/forum/156583/best-way-to-link-bootstrap-file-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 to return the list of [connected devices](https://yourviews.mindstick.com/view/85703/human-centric-iot-design-ensuring-user-friendly-and-inclusive-connected-devices) of computer **GetConnectedUSBDevices()**

```
static List<USBDeviceInfoClass> GetConnectedUSBDevices(){List<USBDeviceInfoClass> _devices = new List<USBDeviceInfoClass>();
// Create      List of USBDeviceInfoClass with _devices namedManagementObjectCollection 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();        }        for each (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#](https://www.mindstick.com/mindstickarticle/a34004cd-29f5-4273-a81a-e2970ea2e48b/images/91739943-e5c2-473e-ad2b-db6dfc1fe2cf.png)

---

Original Source: https://www.mindstick.com/articles/1376/check-device-connect-to-pc-by-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
