Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i cannot detec usb device id is true or false

What I have tried:

hi everybody
i have a simple c# project and i want to my application run after check plugged usb drive,s mac address is true

thanks alot
Posted
Updated 15-May-20 3:42am

1 solution

At a minimum, you would need a service

1) (*possibly*) - with a hidden window/message pump, that could use RegisterDeviceNotification to handle a WM_DEVICECHANGE message for a DBT_DEVICEARRIVAL event
2) (*OR*) poll the Virtual Ports/hardware periodically looking for the device signature
3) And then -> run your program

Simples .. now all you have to do is write the code

You might wish to look at the following code to see if it at least detects/shows your USB device when it's plugged in

namespace ConsoleApplication1
{
  using System;
  using System.Collections.Generic;
  using System.Management; // need to add System.Management to your project references.

  class Program
  {
    static void Main(string[] args)
    {
      var usbDevices = GetUSBDevices();

      foreach (var usbDevice in usbDevices)
      {
        Console.WriteLine("Device ID: {0}, PNP Device ID: {1}, Description: {2}",
            usbDevice.DeviceID, usbDevice.PnpDeviceID, usbDevice.Description);
      }

      Console.Read();
    }

    static List<USBDeviceInfo> GetUSBDevices()
    {
      List<USBDeviceInfo> devices = new List<USBDeviceInfo>();

      ManagementObjectCollection collection;
      using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_PnPEntity"))
        collection = searcher.Get();      

      foreach (var device in collection)
      {
        devices.Add(new USBDeviceInfo(
        (string)device.GetPropertyValue("DeviceID"),
        (string)device.GetPropertyValue("PNPDeviceID"),
        (string)device.GetPropertyValue("Description")
        ));
      }

      collection.Dispose();
      return devices;
    }
  }

  class USBDeviceInfo
  {
    public USBDeviceInfo(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; }
  }
}


If that doesnt work you may need to look at A USB Library to Detect USB Devices[^] and see how it detects devices. There is also Enumerate and Auto-Detect USB Drives[^] and (more sophisticated) GitHub - MelbourneDeveloper/Device.Net: A C# cross platform connected device framework[^] but I'd start with simple first to see if it can find your device, that's 1/2 the battle.

Once you can detect that your particular device is actually there, you could put a timer/poll loop around the code to check for the device every say minute. This is the easiest path - using a hidden c# window is quite advanced.

To be honest, this is a bit more advanced than a beginner task - with a lot of reading and experimentation though, you may be able to cobble something up.
 
Share this answer
 
v3
Comments
OriginalGriff 17-May-20 2:27am    
"So how do I do that because I'm a beginner"

Posted by OP as a solution.
Garth J Lancaster 17-May-20 2:43am    
:face-palm: .. and there's the reason I only gave the notes I did.. ok, thanks, I'll see how much further I can take him

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900