C#获取连接到PC的便携式设备

ban*_*ong 5 c# connection usb wpd

我试图在Windows 7上获取所有USB设备(包括便携式设备),现在我搜遍了所有并没有找到一个好的答案.

我试过这段代码:

static void Main(string[] args)
{
    //
    // Get an instance of the device manager
    //
    PortableDeviceApiLib.PortableDeviceManagerClass devMgr
        = new PortableDeviceApiLib.PortableDeviceManagerClass();

    //
    // Probe for number of devices
    //
    uint cDevices = 1;
    devMgr.GetDevices(null, ref cDevices);

    //
    // Re-allocate if needed
    //
    if (cDevices > 0)
    {
        string[] deviceIDs = new string[cDevices];
        devMgr.GetDevices(deviceIDs, ref cDevices);

        for (int ndxDevices = 0; ndxDevices < cDevices; ndxDevices++)
        {
            Console.WriteLine("Device[{0}]: {1}",
                    ndxDevices + 1, deviceIDs[ndxDevices]);
        }
    }
    else
    {
        Console.WriteLine("No WPD devices are present!");
    }
}
Run Code Online (Sandbox Code Playgroud)

但我得到这个错误:

interop type'portabledeviceapilib.portabledevicemanagerclass'无法嵌入

现在我很困惑.

如果你可以帮助我这个代码/给我一个想法我应该尝试什么,生病快乐

我只需要连接哪种类型的USB,连接手机或鼠标.我想知道连接的是什么.

Thanx Ahead

Cod*_*Fox 3

我正在使用 NuGet 包PortableDevices(基于Christophe Geers 的教程)。

源自教程的第一部分:

public void ListDevices()
{
    var devices = new PortableDeviceCollection();
    devices.Refresh();

    foreach (var device in devices)
    {
        device.Connect();
        Console.WriteLine(@"DeviceId: {0}, FriendlyName: {1}", device.DeviceId, device.FriendlyName);
        device.Disconnect();
    }
}
Run Code Online (Sandbox Code Playgroud)