Getting information about USB and other devices under Windows with C#

I was looking for a way to get information about all connected devices (USB and otherwise) with C#. It turned out it’s not that easy to find out how, considering the following requirements:

  • No admin rights required
  • Return all devices, including those connected via USB hubs or additional USB cards
  • Only standard Windows API

But the solution itself is pretty simple:

    public class WinDevices
    {
        static public List<DeviceInfo> GetUSBDevices()
        {
            List<DeviceInfo> devices = new List<DeviceInfo>();
 
            ManagementObjectCollection collection;
            using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_PnPEntity"))
                collection = searcher.Get();
 
 
            foreach (var device in collection)
            {
                var deviceInfo = new DeviceInfo();
                deviceInfo.DeviceID =       (string)device.GetPropertyValue("DeviceID");
                deviceInfo.PNPDeviceID =    (string)device.GetPropertyValue("PNPDeviceID");
                deviceInfo.Description =    (string)device.GetPropertyValue("Description");
                deviceInfo.Name =           (string)device.GetPropertyValue("Name");
                deviceInfo.Caption =        (string)device.GetPropertyValue("Caption");
                deviceInfo.Service =        (string)device.GetPropertyValue("Service");
                devices.Add(deviceInfo);

                // Other properties supported by Win32_PnPEntity
                // See http://msdn.microsoft.com/en-us/library/aa394353%28v=vs.85%29.aspx
                //var keys = new string[] {
                //        "Availability",
                //        "Caption",
                //        "ClassGuid",
                //        "CompatibleID[]",
                //        "ConfigManagerErrorCode",
                //        "ConfigManagerUserConfig",
                //        "CreationClassName",
                //        "Description",
                //        "DeviceID",
                //        "ErrorCleared",
                //        "ErrorDescription",
                //        "HardwareID[]",
                //        "InstallDate",
                //        "LastErrorCode",
                //        "Manufacturer",
                //        "Name",
                //        "PNPDeviceID",
                //        "PowerManagementCapabilities[]",
                //        "PowerManagementSupported",
                //        "Service",
                //        "Status",
                //        "StatusInfo",
                //        "SystemCreationClassName",
                //        "SystemName"
                //};

            }
 
            collection.Dispose();
            return devices;
        }
 
        public class DeviceInfo
        {
            public string Name              { get; set; }
            public string DeviceID          { get; set; }
            public string PNPDeviceID       { get; set; }
            public string Description       { get; set; }
            public string Caption           { get; set; }
            public string Service           { get; set; }
        }
    }
 
Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s