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 an 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 to 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;
}
publicstring DeviceID { get; privateset; }
publicstring PnpDeviceID { get; privateset; }
publicstring Description { get; privateset; }
}
privatevoid 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;
}
staticList<USBDeviceInfoClass> GetConnectedUSBDevices()
{
List<USBDeviceInfoClass> _devices = newList<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 = newManagementObjectSearcher(@"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(newUSBDeviceInfoClass(
(string)device.GetPropertyValue("DeviceID"),
(string)device.GetPropertyValue("PNPDeviceID"),
(string)device.GetPropertyValue("Description")
));
}
collection.Dispose();
return _devices;
}
}
Output: -
Leave Comment