简体   繁体   中英

Detecting USB drive insertion and removal using windows service and Vb.Net

I want in my application to detect if a USB Drive is plugged in or plugged out.

I have googled a lot about it and actually found a lot answers but none worked exactly how I wanted it to be. I found one that works perfectly and give message when a drive is plugged in or plugged out but that uses WndProc which is a very long procedure and very hard to understand specially to me who have zero knowledge about it but that isn't the main problem. The main problem I am founding with WndProc is that it cannot do some functions that I want to do whereas the WMI can do those. I also found the WMI solution, which can detect a drive when a drive is plugged in but it cannot detect when a device is plugged out which is as very important for my program. I found another solution which seems to work but it is in C# code and I tried to convert it to VB.Net but was failed to do so when I came in code line 4 of that C# code (Which I will add later in the question below).

Links to the solutions that helped me partially:

  • WndProc - www.vbforfree.com

    Detects drive plugged in and plugged out event perfectly.

  • WMI Solution Vb.Net - www.vb-tips.com

    Works perfectly when a drive plugged in but cannot detect if a drive is plugged out.

  • WMI Solution C# - stackoverflow.com

    Seems to work but failed to convert it to Vb.Net

C# code that I guess might work:

using System.Management;

ManagementEventWatcher watcher = new ManagementEventWatcher();
WqlEventQuery query = new WqlEventQuery("SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2");
//I am stuck from the line below this
watcher.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
watcher.Query = query;
watcher.Start();
watcher.WaitForNextEvent();

I found the solution :)

Ref.

Win32_VolumeChangeEvent class

  • Configuration Changed (1)
  • Device Arrival (2)
  • Device Removal (3)
  • Docking (4)

Code:

Imports System.Management
Imports Microsoft.Win32

Public Class Form1
    Dim WithEvents pluggedInWatcher As ManagementEventWatcher
    Dim WithEvents pluggedOutWatcher As ManagementEventWatcher
    Dim pluggedInQuery As WqlEventQuery
    Dim pluggedOutQuery As WqlEventQuery

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            pluggedInQuery = New WqlEventQuery
            pluggedInQuery.QueryString = "SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2"
            pluggedInWatcher = New ManagementEventWatcher(pluggedInQuery)
            pluggedInWatcher.Start()

            pluggedOutQuery = New WqlEventQuery
            pluggedOutQuery.QueryString = "SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 3"
            pluggedOutWatcher = New ManagementEventWatcher(pluggedOutQuery)
            pluggedOutWatcher.Start()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub pluggedInWatcher_EventArrived(sender As Object, e As EventArrivedEventArgs) Handles pluggedInWatcher.EventArrived
        MsgBox("Plugged In")
    End Sub

    Private Sub pluggedOutWatcher_EventArrived(sender As Object, e As EventArrivedEventArgs) Handles pluggedOutWatcher.EventArrived
        MsgBox("Plugged Out")
    End Sub
End Class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM