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

通过USB或其他方式连接新驱动器时获取通知(C#)

作为一个云计算领域的专家,我可以告诉你,通过USB或其他方式连接新驱动器时获取通知可以通过C#编程语言实现。

在C#中,可以使用Windows API来监控USB设备的连接和断开事件。具体来说,可以使用RegisterDeviceNotification函数来注册设备通知,并在设备连接或断开时接收通知。

以下是一个简单的示例代码,演示如何使用C#监控USB设备的连接和断开事件:

代码语言:csharp
复制
using System;
using System.Runtime.InteropServices;

public class UsbDeviceWatcher
{
    public delegate void UsbDeviceEventHandler(object sender, UsbDeviceEventArgs e);

    public event UsbDeviceEventHandler UsbDeviceConnected;
    public event UsbDeviceEventHandler UsbDeviceDisconnected;

    private IntPtr _windowHandle;
    private GCHandle _thisHandle;

    public UsbDeviceWatcher()
    {
        _thisHandle = GCHandle.Alloc(this);
        _windowHandle = NativeMethods.CreateWindowEx(0, NativeMethods.WC_NOTIFY, "", 0, 0, 0, 0, 0, NativeMethods.HWND_MESSAGE, 0, 0, 0);
        NativeMethods.DEV_BROADCAST_DEVICEINTERFACE dbi = new NativeMethods.DEV_BROADCAST_DEVICEINTERFACE();
        dbi.dbcc_size = Marshal.SizeOf(dbi);
        dbi.dbcc_devicetype = NativeMethods.DBT_DEVTYP_DEVICEINTERFACE;
        IntPtr buffer = Marshal.AllocHGlobal(dbi.dbcc_size);
        Marshal.StructureToPtr(dbi, buffer, true);
        NativeMethods.RegisterDeviceNotification(_windowHandle, buffer, NativeMethods.DEVICE_NOTIFY_WINDOW_HANDLE);
        Marshal.FreeHGlobal(buffer);
    }

    ~UsbDeviceWatcher()
    {
        NativeMethods.DestroyWindow(_windowHandle);
        _thisHandle.Free();
    }

    private void OnUsbDeviceEvent(UsbDeviceEventArgs e)
    {
        if (e.Connected)
        {
            UsbDeviceConnected?.Invoke(this, e);
        }
        else
        {
            UsbDeviceDisconnected?.Invoke(this, e);
        }
    }

    private const int WM_DEVICECHANGE = 0x0219;

    private void WndProc(ref System.Windows.Forms.Message m)
    {
        if (m.Msg == WM_DEVICECHANGE)
        {
            UsbDeviceEventArgs e = new UsbDeviceEventArgs();
            e.Connected = (m.WParam.ToInt32() == NativeMethods.DBT_DEVICEARRIVAL);
            e.DevicePath = Marshal.PtrToStringAuto(m.LParam);
            OnUsbDeviceEvent(e);
        }
    }

    private class NativeMethods
    {
        public const int WM_DEVICECHANGE = 0x0219;
        public const int DBT_DEVICEARRIVAL = 0x8000;
        public const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
        public const int DBT_DEVTYP_DEVICEINTERFACE = 5;
        public const int DEVICE_NOTIFY_WINDOW_HANDLE = 0;
        public const int HWND_MESSAGE = -3;
        public const string WC_NOTIFY = "WC_NOTIFY";

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern IntPtr CreateWindowEx(int dwExStyle, string lpClassName, string lpWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool DestroyWindow(IntPtr hWnd);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern IntPtr RegisterDeviceNotification(IntPtr hRecipient, IntPtr NotificationFilter, int Flags);

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

public class UsbDeviceEventArgs : EventArgs
{
    public bool Connected { get; set; }
    public string DevicePath { get; set; }
}

在上面的代码中,UsbDeviceWatcher类监控USB设备的连接和断开事件,并在事件发生时触发UsbDeviceConnectedUsbDeviceDisconnected事件。UsbDeviceEventArgs类包含了设备连接状态和设备路径等信息。

使用UsbDeviceWatcher类非常简单,只需要创建一个实例并订阅UsbDeviceConnectedUsbDeviceDisconnected事件即可:

代码语言:csharp
复制
UsbDeviceWatcher watcher = new UsbDeviceWatcher();
watcher.UsbDeviceConnected += (sender, e) => Console.WriteLine($"USB device connected: {e.DevicePath}");
watcher.UsbDeviceDisconnected += (sender, e) => Console.WriteLine($"USB device disconnected: {e.DevicePath}");

需要注意的是,上面的代码只能在Windows操作系统上运行,并且需要引用System.Windows.FormsSystem.Runtime.InteropServices命名空间。

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

相关·内容

领券