2

Is it possible programmatically (using C# or PowerShell) to find out when a USB device was mounted? I want to find out when a USB printer was plugged into a machine or turned on?

wonea
  • 4,783
  • 17
  • 86
  • 139
  • If you application is running constantly in the background you could monitor all USB plugged devices and record times yourself. But what if the device is already plugged when computer was turned on? I don't think you'll be able to find the time in this case. – RaYell Jan 12 '15 at 11:05
  • Possible duplicate of http://stackoverflow.com/questions/17912969/detect-when-a-new-virtual-drive-is-created – dotNET Jan 12 '15 at 11:06
  • Yes, but the printer has no mountable drive. It is only a device. – wonea Jan 12 '15 at 11:26

1 Answers1

2

You can use the following codes to detect device changing. But I don't know how to define the changed device is an USB printer or not.

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

    protected override void WndProc(ref Message m)
    {
        const int WM_DEVICECHANGE = 0x0219;
        switch (m.Msg)
        {
            case WM_DEVICECHANGE:

                MessageBox.Show("Something changed.");

                break;
            default:
                break;
        }
        base.WndProc(ref m);
    }
}
iBener
  • 469
  • 6
  • 18
  • if you put a breakpoint on `MessageBox.Show` and when you plug-in something on your computer if you watch the `Message` parameter values maybe you can find the correct value for the USB printer. – iBener Jan 12 '15 at 12:35
  • I don't think the `Form` class subscribes to that particular message. – user692942 Jan 12 '15 at 13:42
  • I used that way one of my project to detecting USB flash drive inserting. It should be work when a device attached to the computer. – iBener Jan 12 '15 at 14:59