How to get a user-friendly device name on device arrival event

Okay, this is more advanced, because here we start to combine several features of the library. This example works for all kind of devices, not only for volumes. The sample application below registers for device arrival notifications using an ordinary DeviceNotifcationHandler. The application adds a handler for the DeviceArrival-event and subscribes for device change notifications using the DeviceType.Interface-flag.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        InitializeDeviceNotifications();
    }

    private void InitializeDeviceNotifications()
    {
        var notificationHandler = new DeviceNotificationHandler();

        notificationHandler.DeviceArrival += new EventHandler<DeviceNotificationEventArgs>(notificationHandler_DeviceArrival);

        var deviceNotification = new DeviceNotification() { NotificationHandler = notificationHandler };

        deviceNotification.Register(DeviceType.Interface);

        this.components.Add(deviceNotification);
    }

    void notificationHandler_DeviceArrival(object sender, DeviceNotificationEventArgs e)
    {
    //  TODO: We need to do something in here (-:
    }
}

The event which is getting fired when a new device arrives, won´t give any detailed information about the device - except the device interface class and path. If we want to determine a user-friendly device name (which can be shown in the UI), we must iterate all present devices of the given interface class and compare each device by using the path. At first, let´s have a look at the code, which queries a list of all devices of a certain interface class. We will map the matching device to a simple model-class, because we want to free all unmanaged resources when we´re done.

class Device
{
    public Guid InterfaceGuid { get; set; }

    public string DevicePath { get; set; }

    public string DisplayName { get; set; }
}

...

static Device GetDevice(DeviceInterface deviceInterface)
{
    using (DeviceInfoSet set = DeviceInfoSet.GetPresent(deviceInterface))
    {
        foreach (DeviceInfo device in set.GetDevices())
        {
            if (set.HasInterface())
            {
                IEnumerable<DeviceInterfaceData> deviceInterfaceData = set.GetDeviceInterfaceData(device);
                DeviceInterfaceData matchingInterfaceData;
                if ((matchingInterfaceData = (deviceInterfaceData.Where((x) => CompareDevicePath(x.DevicePath, deviceInterface.Name))).SingleOrDefault()) != null)
                    return new Device() { InterfaceGuid = deviceInterface.ClassGuid, DevicePath = matchingInterfaceData.DevicePath, DisplayName = device.FriendlyName };
            }
        }
    }

    return null;
}

static bool CompareDevicePath(string path1, string path2)
{
    if (string.IsNullOrEmpty(path1) || (string.IsNullOrEmpty(path2)))
        return false;

    return path1.ToLower().Equals(path2.ToLower());
}

Now, we will use this functionality in the event-handler.

void notificationHandler_DeviceArrival(object sender, DeviceNotificationEventArgs e)
{
    if (e.IsInterface())
    {
        DeviceInterface deviceInterface = e.GetInterface();

            Device dev = GetDevice(deviceInterface);
            if ((dev != null) && (!string.IsNullOrEmpty(dev.DisplayName)))
                MessageBox.Show(string.Format("The following device has arrived: {0}", dev.DisplayName));

    }
}

That´s it.

Last edited Aug 28, 2011 at 8:09 PM by mfriedrich, version 7

Comments

No comments yet.