469,328 Members | 1,294 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

How to detect if a device is connected to PC using USB or Serial Port?

Hi All,

I have to deal with some devices like Pen Tablet, WebCam etc to work for a product.
Now my Problem is some times while working , I am not able to tell whether a certain device is connected to PC .

So , Is there any way in C# where i can detect if device is connected using USB port or serial port.
Any help will be useful.

Thanks in Advance
Eric.
Sep 9 '09 #1
3 26013
madankarmukta
308 256MB
@Charming12
It seems that you need to use the System.Runtime.InteropServices namespace to come to the result.The solution needs to make use of the unmanaged code.

Please refer the link below
http://stackoverflow.com/questions/2...n-is-connected

Thanks!
Sep 9 '09 #2
Plater
7,872 Expert 4TB
Do you want to know if the device is attached via serial OR usb, or you want to find any devices attached to either port type?
You can probably accomplish that using something from the WMI libraries.
Look for something called "WMICodeCreator", its really usefull for playing around and seeing what you can find.

For example using WIn32_PnPEntity it made this code:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Management;
  3. using System.Windows.Forms;
  4.  
  5. namespace WMISample
  6. {
  7.     public class MyWMIQuery
  8.     {
  9.         public static void Main()
  10.         {
  11.             try
  12.             {
  13.                 ManagementObjectSearcher searcher = 
  14.                     new ManagementObjectSearcher("root\\CIMV2", 
  15.                     "SELECT * FROM Win32_PnPEntity"); 
  16.  
  17.                 foreach (ManagementObject queryObj in searcher.Get())
  18.                 {
  19.                     Console.WriteLine("-----------------------------------");
  20.                     Console.WriteLine("Win32_PnPEntity instance");
  21.                     Console.WriteLine("-----------------------------------");
  22.                     Console.WriteLine("Caption: {0}", queryObj["Caption"]);
  23.                     Console.WriteLine("Description: {0}", queryObj["Description"]);
  24.                     Console.WriteLine("DeviceID: {0}", queryObj["DeviceID"]);
  25.                 }
  26.             }
  27.             catch (ManagementException e)
  28.             {
  29.                 MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
  30.             }
  31.         }
  32.     }
  33. }
  34.  
Producing something like:
Win32_PnPEntity instance
-----------------------------------
Caption: 802.11n USB Wireless LAN Card - Deterministic Network Enhancer Miniport

Description: Deterministic Network Enhancer Miniport
DeviceID: ROOT\DNI_DNEMP\0007
-----------------------------------
Win32_PnPEntity instance
-----------------------------------
Caption: 802.11n USB Wireless LAN Card - Virtual Machine Network Services Driver

Description: Virtual Machine Network Services Driver
DeviceID: ROOT\CNTX_VPCNETS2_MP\0004
-----------------------------------
Win32_PnPEntity instance
-----------------------------------
Caption: 802.11n USB Wireless LAN Card - Packet Scheduler Miniport
Description: Packet Scheduler Miniport
DeviceID: ROOT\MS_PSCHEDMP\0007
Sep 9 '09 #3
Thanks a lot,

I will give it a try, and will surely comeback if it works
Sep 9 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Dhananjay | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by listenups61195 | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.