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;
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.