Device Notification

LibUsbDotNet

LibUsbDotNet 2.2.8 Device Notification
LibUsbDotNet on SourceForge

System-level Device Notification: Console Application Description
  1. Creates a DeviceNotifier instance for monitoring system-level device events.

  2. Displays device events in console ouput until a key is pressed.

Example
CopyC#
using System;
using System.Windows.Forms;
using LibUsbDotNet.DeviceNotify;

namespace Device.Notification
{
    internal class DeviceNotification
    {
        public static IDeviceNotifier UsbDeviceNotifier = DeviceNotifier.OpenDeviceNotifier();

        private static void Main(string[] args)
        {
            // Hook the device notifier event
            UsbDeviceNotifier.OnDeviceNotify += OnDeviceNotifyEvent;

            // Exit on and key pressed.
            Console.Clear();            
            Console.WriteLine();
            Console.WriteLine("Waiting for system level device events..");
            Console.Write("[Press any key to exit]");

            while (!Console.KeyAvailable)
                Application.DoEvents();

            UsbDeviceNotifier.Enabled = false;  // Disable the device notifier

            // Unhook the device notifier event
            UsbDeviceNotifier.OnDeviceNotify -= OnDeviceNotifyEvent;
        }

        private static void OnDeviceNotifyEvent(object sender, DeviceNotifyEventArgs e)
        {
            // A Device system-level event has occured

            Console.SetCursorPosition(0,Console.CursorTop);

            Console.WriteLine(e.ToString()); // Dump the event info to output.

            Console.WriteLine();
            Console.Write("[Press any key to exit]");
        }
    }
}
Example
CopyVB.NET
Imports System
Imports System.Windows.Forms
Imports LibUsbDotNet.DeviceNotify

Namespace Device.Notification
    Friend Class DeviceNotification
        Public Shared UsbDeviceNotifier As IDeviceNotifier = DeviceNotifier.OpenDeviceNotifier()

        Private Shared Sub Main(args As String())
            ' Hook the device notifier event
            AddHandler UsbDeviceNotifier.OnDeviceNotify, AddressOf OnDeviceNotifyEvent

            ' Exit on and key pressed.
            Console.Clear()
            Console.WriteLine()
            Console.WriteLine("Waiting for system level device events..")
            Console.Write("[Press any key to exit]")

            While Not Console.KeyAvailable
                Application.DoEvents()
            End While

            UsbDeviceNotifier.Enabled = False
            ' Disable the device notifier
            ' Unhook the device notifier event
            RemoveHandler UsbDeviceNotifier.OnDeviceNotify, AddressOf OnDeviceNotifyEvent
        End Sub

        Private Shared Sub OnDeviceNotifyEvent(sender As Object, e As DeviceNotifyEventArgs)
            ' A Device system-level event has occured


            Console.SetCursorPosition(0, Console.CursorTop)

            Console.WriteLine(e.ToString())
            ' Dump the event info to output.
            Console.WriteLine()
            Console.Write("[Press any key to exit]")
        End Sub
    End Class
End Namespace
Compiling the Code
  1. Create a new console application in your favorite designer.

  2. Verify your project references:

    • System.dll

    • LibUsbDotNet.dll

  3. Add/edit the main class. Copy/Paste code from one of the examples above.