Проверить изменения устройства (добавить / удалить) события

Я хотел бы знать, есть ли способ вызвать событие, когда устройство добавляется или удаляется из системы. Я хочу быть в состоянии определить, если, скажем, была добавлена ​​флешка, или мышь, или что-то еще. Я пытался искать вокруг, но я не могу найти ничего, что говорит о том, как это сделать.

Есть идеи?

6 ответов

Решение

Если у вас есть окно в вашем приложении, вы можете использовать что-то вроде этого:

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 Forms):

    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 Forms (еще проще):

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

Принятый ответ отличный, однако он работает только с USB-устройствами.

Чтобы заставить его работать со всеми устройствами (и дополнительно фильтровать USB), используйте следующий слегка измененный класс:

static class DeviceNotification {
    //https://msdn.microsoft.com/en-us/library/aa363480(v=vs.85).aspx
    public const int DbtDeviceArrival = 0x8000; // system detected a new device        
    public const int DbtDeviceRemoveComplete = 0x8004; // device is gone     
    public const int DbtDevNodesChanged = 0x0007; //A device has been added to or removed from the system.

    public const int WmDevicechange = 0x0219; // device change event      
    private const int DbtDevtypDeviceinterface = 5;
    //https://msdn.microsoft.com/en-us/library/aa363431(v=vs.85).aspx
    private const int DEVICE_NOTIFY_ALL_INTERFACE_CLASSES = 4;
    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 devices are plugged or unplugged.
    /// </summary>
    /// <param name="windowHandle">Handle to the window receiving notifications.</param>
    /// <param name="usbOnly">true to filter to USB devices only, false to be notified for all devices.</param>
    public static void RegisterDeviceNotification(IntPtr windowHandle, bool usbOnly = false) {
        var 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, usbOnly ? 0 : DEVICE_NOTIFY_ALL_INTERFACE_CLASSES);
    }

    /// <summary>
    /// Unregisters the window for device notifications
    /// </summary>
    public static void UnregisterDeviceNotification() {
        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;
    }
}

Главное изменение - это Flags параметр при звонке RegisterDeviceNotification (см. https://msdn.microsoft.com/en-us/library/aa363431(v=vs.85).aspx), который, если установлено 4 вместо 0 будет игнорировать ClassGuid Параметр и зарегистрируйтесь для всех устройств.

Вот версия получше, потому что она может получить имя порта. В случае типа устройства DBT_DEVTYP_PORT lParam указывает на DEV_BROADCAST_PORT, который содержит DEV_BROADCAST_HDR, а затем имя в Unicode удаляемого или добавляемого устройства, заканчивающееся нулями.

          protected override void WndProc(ref Message m)
    {
        switch ((WndMessage) m.Msg)
        {
            case WndMessage.WM_DEVICECHANGE:
                DEV_BROADCAST_HDR dbh;

                switch ((WM_DEVICECHANGE) m.WParam)
                {
                    case WM_DEVICECHANGE.DBT_DEVICEARRIVAL:
                    case WM_DEVICECHANGE.DBT_DEVICEREMOVECOMPLETE:
                        dbh = (DEV_BROADCAST_HDR) Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_HDR));
                        if ((WM_DEVICECHANGE) dbh.dbch_devicetype == WM_DEVICECHANGE.DBT_DEVTYP_PORT)
                        {
                            var portNameBytes = new byte[dbh.dbch_size - (int) WM_DEVICECHANGE.SIZE_OF_DBH];
                            Marshal.Copy(m.LParam + (int) WM_DEVICECHANGE.SIZE_OF_DBH, portNameBytes, 0, portNameBytes.Length);
                            string portName = Encoding.Unicode.GetString(portNameBytes).TrimEnd('\0');
                            if (portName == Settings.Instance.PortName)
                            {
                                if ((WM_DEVICECHANGE) m.WParam == WM_DEVICECHANGE.DBT_DEVICEREMOVECOMPLETE)
                                {
                                    if (!_port.IsOpen)
                                    {
                                        ClosePort();
                                    }
                                }
                                else
                                {
                                    BeginInvoke((Action) (() => OpenPort()));
                                }
                            }
                        }
                        break;
                }
                break;
        }

        base.WndProc(ref m);
    }

public enum WndMessage
{
    WM_DEVICECHANGE = 0x0219, // device change event   
}

public enum WM_DEVICECHANGE
{
    // full list: https://docs.microsoft.com/en-us/windows/win32/devio/wm-devicechange
    DBT_DEVICEARRIVAL = 0x8000,             // A device or piece of media has been inserted and is now available.
    DBT_DEVICEREMOVECOMPLETE = 0x8004,      // A device or piece of media has been removed.

    DBT_DEVTYP_DEVICEINTERFACE = 0x00000005,    // Class of devices. This structure is a DEV_BROADCAST_DEVICEINTERFACE structure.
    DBT_DEVTYP_HANDLE = 0x00000006,             // File system handle. This structure is a DEV_BROADCAST_HANDLE structure.
    DBT_DEVTYP_OEM = 0x00000000,                // OEM- or IHV-defined device type. This structure is a DEV_BROADCAST_OEM structure.
    DBT_DEVTYP_PORT = 0x00000003,               // Port device (serial or parallel). This structure is a DEV_BROADCAST_PORT structure.
    DBT_DEVTYP_VOLUME = 0x00000002,             // Logical volume. This structure is a DEV_BROADCAST_VOLUME structure.

    SIZE_OF_DBH = 12,   // sizeof(DEV_BROADCAST_HDR)
}

public struct DEV_BROADCAST_HDR
{
    internal UInt32 dbch_size;
    internal UInt32 dbch_devicetype;
    internal UInt32 dbch_reserved;
};

Я пришел к этому сообщению для более конкретного случая, чем первоначальный вопрос, в котором я хочу получать уведомления каждый раз, когда порт добавляется или удаляется. Для этой ситуации ответ намного проще и не требует вызова RegisterDeviceNotification:

События DBT_DEVICEARRIVAL и DBT_DEVICEREMOVECOMPLETE автоматически транслируются во все окна верхнего уровня для устройств портов. Поэтому нет необходимости вызывать RegisterDeviceNotification для портов....

https://docs.microsoft.com/en-us/windows/desktop/api/Winuser/nf-winuser-registerdevicenotificationa

Таким образом, решение становится примерно таким:

using System.Runtime.InteropServices;

//Put all of the following code inside your Form's partial class:

private struct DEV_BROADCAST_HDR {
    internal UInt32 dbch_size;
    internal UInt32 dbch_devicetype;
    internal UInt32 dbch_reserved;
};

protected override void WndProc(ref Message m) {
    base.WndProc(ref m);        //This allows window default behavior of base class to be executed
    if (m.Msg == 0x0219) {      //WM_DEVICECHANGE = 0x0219
        DEV_BROADCAST_HDR dbh;
        switch ((int)m.WParam) {                    
            case 0x8000:        //DBT_DEVICEARRIVAL = 0x8000
                dbh = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_HDR));
                if (dbh.dbch_devicetype == 0x00000003) {     //DBT_DEVTYP_PORT = 0x00000003
                    Console.WriteLine("Port added!");
                    //TODO
                }
                break;
            case 0x8004:        //DBT_DEVICEREMOVECOMPLETE = 0x8004                     
                dbh = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_HDR));
                if (dbh.dbch_devicetype == 0x00000003) {     //DBT_DEVTYP_PORT = 0x00000003
                    Console.WriteLine("Port removed!");
                    //TODO
                }
                break;
        }
    }
}

Как упоминал DatuPuti :

События DBT_DEVICEARRIVAL и DBT_DEVICEREMOVECOMPLETE автоматически транслируются во все окна верхнего уровня для портовых устройств. Следовательно, нет необходимости вызывать RegisterDeviceNotification для портов.

Я взял его реализацию и адаптировал для Windows Forms:

      using System;
using System.Runtime.InteropServices;

private enum WM_DEVICECHANGE
{
    // full list: https://docs.microsoft.com/en-us/windows/win32/devio/wm-devicechange
    DBT_DEVICEARRIVAL = 0x8000,             // A device or piece of media has been inserted and is now available.
    DBT_DEVICEREMOVECOMPLETE = 0x8004,      // A device or piece of media has been removed.
}
private int WmDevicechange = 0x0219; // device change event   

protected override void WndProc(ref Message m)
{
    base.WndProc(ref m);        //This allows window default behavior of base class to be executed

    if (m.Msg == WmDevicechange)
    {
        switch ((WM_DEVICECHANGE)m.WParam)
        {
            case WM_DEVICECHANGE.DBT_DEVICEREMOVECOMPLETE:
                Console.WriteLine("USB Device removed");
                break;
            case WM_DEVICECHANGE.DBT_DEVICEARRIVAL:
                Console.WriteLine("USB Device added");
                break;
        }
    }
}

Если у вас нет окна, это решение создает общее окно без имени класса и его дескриптора:

      using System;
using System.Windows.Forms;

internal class UsbService : NativeWindow, IDisposable
{
    internal UsbService(IEventAggregator eventAggregator)
    {
        _eventAggregator = eventAggregator;
        base.CreateHandle(new CreateParams());
    }

    protected override void WndProc(ref Message msg)
    {
        base.WndProc(ref msg);
        if (msg.Msg == 0x0219) // Device change event
        {
            switch (msg.WParam.ToInt32())
            {
                case 0x8000: // Device added
                case 0x8004: // Device removed
                    _eventAggregator.Publish(...);
                    break;
            }
        }
    }

    public void Dispose()
    {
        if (!_isDisposed)
        {
            base.DestroyHandle();
            _isDisposed = true;
            GC.SuppressFinalize(this);
        }
    }

    private bool _isDisposed;
    private IEventAggregator _eventAggregator;
}

Я думаю, что чище использовать это в проекте WPF. В этом примере я использовал DI для внедрения агрегатора событий, который будет публиковать событие, на которое я подписан, в другом классе. Это можно удалить и заменить событиями, чтобы уведомить об изменении. Чтобы получить основную и быструю информацию о подключенных томах, я бы использовал метод DriveInfo.GetDrives() .

Другие вопросы по тегам