栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

检查设备更改(添加/删除)事件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

检查设备更改(添加/删除)事件

如果您的应用程序中有一个窗口,则可以使用以下方法:

using System;using System.Runtime.InteropServices;internal static class UsbNotification{    public const int DbtDevicearrival = 0x8000; // system detected a new device public const int DbtDeviceremovecomplete = 0x8004; // device is gone          public const int WmDevicechange = 0x0219; // device change event          private const int DbtDevtypDeviceinterface = 5;    private static readonly Guid GuidDevinterfaceUSBDevice = new Guid("A5DCBF10-6530-11D2-901F-00C04FB951ED"); // USB devices    private static IntPtr notificationHandle;    /// <summary>    /// Registers a window to receive notifications when USB devices are plugged or unplugged.    /// </summary>    /// <param name="windowHandle">Handle to the window receiving notifications.</param>    public static void RegisterUsbDeviceNotification(IntPtr windowHandle)    {        DevBroadcastDeviceinterface dbi = new DevBroadcastDeviceinterface        { DeviceType = DbtDevtypDeviceinterface, Reserved = 0, ClassGuid = GuidDevinterfaceUSBDevice, Name = 0        };        dbi.Size = Marshal.SizeOf(dbi);        IntPtr buffer = Marshal.AllocHGlobal(dbi.Size);        Marshal.StructureToPtr(dbi, buffer, true);        notificationHandle = RegisterDeviceNotification(windowHandle, buffer, 0);    }    /// <summary>    /// Unregisters the window for USB device notifications    /// </summary>    public static void UnregisterUsbDeviceNotification()    {        UnregisterDeviceNotification(notificationHandle);    }    [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 DevBroadcastDeviceinterface    {        internal int Size;        internal int DeviceType;        internal int Reserved;        internal Guid ClassGuid;        internal short Name;    }}

在WPF窗口中使用它的方式如下(Windows窗体类似):

    protected override void onSourceInitialized(EventArgs e)    {        base.onSourceInitialized(e);        // Adds the windows message processing hook and registers USB device add/removal notification.        HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);        if (source != null)        { windowHandle = source.Handle; source.AddHook(HwndHandler); UsbNotification.RegisterUsbDeviceNotification(windowHandle);        }    }    /// <summary>    /// Method that receives window messages.    /// </summary>    private IntPtr HwndHandler(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam, ref bool handled)    {        if (msg == UsbNotification.WmDevicechange)        { switch ((int)wparam) {     case UsbNotification.DbtDeviceremovecomplete:         Usb_DeviceRemoved(); // this is where you do your magic         break;     case UsbNotification.DbtDevicearrival:         Usb_DeviceAdded(); // this is where you do your magic         break; }        }        handled = false;        return IntPtr.Zero;    }

这是Windows窗体的使用示例(甚至更简单):

public Form1(){    InitializeComponent();    UsbNotification.RegisterUsbDeviceNotification(this.Handle);}protected override void WndProc(ref Message m){    base.WndProc(ref m);        if (m.Msg == UsbNotification.WmDevicechange)    {        switch ((int)m.WParam)        { case UsbNotification.DbtDeviceremovecomplete:     Usb_DeviceRemoved(); // this is where you do your magic     break; case UsbNotification.DbtDevicearrival:     Usb_DeviceAdded(); // this is where you do your magic     break;        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/416851.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号