首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何检测USB断开事件?(Windows,.NET C#应用程序)

要检测USB设备断开事件,您可以使用Windows操作系统的设备管理器API。以下是一个简单的示例,说明如何使用C#编写的.NET应用程序来检测USB设备断开事件。

首先,您需要添加以下命名空间:

代码语言:csharp
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

然后,您可以使用以下代码来检测USB设备断开事件:

代码语言:csharp
复制
public class UsbDeviceNotification
{
    public delegate void UsbDeviceEventHandler(object sender, UsbDeviceEventArgs e);

    public event UsbDeviceEventHandler UsbDeviceDisconnected;

    public void Start()
    {
        // 注册设备接口通知
        RegisterForDeviceNotifications(IntPtr.Zero);
    }

    public void Stop()
    {
        // 注销设备接口通知
        UnregisterDeviceNotifications();
    }

    private IntPtr _deviceNotificationHandle;

    private void RegisterForDeviceNotifications(IntPtr windowHandle)
    {
        var deviceInterfaceClassGuid = new Guid("{A5DCBF10-6530-11D2-901F-00C04FB951ED}"); // 系统设备接口GUID

        var deviceChangeFilter = new DEV_BROADCAST_DEVICEINTERFACE
        {
            dbcc_size = Marshal.SizeOf(typeof(DEV_BROADCAST_DEVICEINTERFACE)),
            dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE,
            dbcc_classguid = deviceInterfaceClassGuid,
        };

        var deviceChangeFilterSize = Marshal.SizeOf(deviceChangeFilter);
        var deviceChangeFilterBuffer = Marshal.AllocHGlobal(deviceChangeFilterSize);
        Marshal.StructureToPtr(deviceChangeFilter, deviceChangeFilterBuffer, true);

        _deviceNotificationHandle = RegisterDeviceNotification(windowHandle, deviceChangeFilterBuffer, DEVICE_NOTIFY_WINDOW_HANDLE);

        Marshal.FreeHGlobal(deviceChangeFilterBuffer);
    }

    private void UnregisterDeviceNotifications()
    {
        if (_deviceNotificationHandle != IntPtr.Zero)
        {
            UnregisterDeviceNotification(_deviceNotificationHandle);
            _deviceNotificationHandle = IntPtr.Zero;
        }
    }

    private void OnUsbDeviceDisconnected(UsbDeviceEventArgs e)
    {
        UsbDeviceDisconnected?.Invoke(this, e);
    }

    private const int DBT_DEVTYP_DEVICEINTERFACE = 0x00000005;
    private const int DEVICE_NOTIFY_WINDOW_HANDLE = 0x00000000;

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr RegisterDeviceNotification(IntPtr recipient, IntPtr notificationFilter, int flags);

    [DllImport("user32.dll")]
    private static extern bool UnregisterDeviceNotification(IntPtr handle);

    [StructLayout(LayoutKind.Sequential)]
    private struct DEV_BROADCAST_DEVICEINTERFACE
    {
        internal int dbcc_size;
        internal int dbcc_devicetype;
        internal int dbcc_reserved;
        internal Guid dbcc_classguid;
        internal short dbcc_name;
    }
}

public class UsbDeviceEventArgs : EventArgs
{
    public string DeviceName { get; set; }
}

要使用此类,请创建一个新的UsbDeviceNotification实例,并订阅UsbDeviceDisconnected事件。当USB设备断开连接时,事件将触发。

代码语言:csharp
复制
var usbDeviceNotification = new UsbDeviceNotification();
usbDeviceNotification.UsbDeviceDisconnected += OnUsbDeviceDisconnected;

usbDeviceNotification.Start();

// 当不再需要检测USB设备断开事件时,调用以下方法
usbDeviceNotification.Stop();

private void OnUsbDeviceDisconnected(object sender, UsbDeviceEventArgs e)
{
    Console.WriteLine($"USB设备已断开连接: {e.DeviceName}");
}

请注意,此示例仅适用于Windows操作系统。如果您需要在其他平台上检测USB设备断开事件,请考虑使用跨平台解决方案,如LibUsbDotNet

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

没有搜到相关的视频

领券