2

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 notice this question was posted by overwriting an old one, on the basis that you have been automatically question banned. It's OK in this case, since you had not received answers to the old question. However, note that in general we discourage this practice - if you have received answers then modifying the question completely will unfairly invalidate other people's work, and you can expect your old question to be rolled back. If you want to ask new Stack Overflow questions, I suggest you get your question ban rescinded, rather than trying to work around it. – halfer Oct 23 '17 at 20:09
  • If you wish to do that, [this is the key resource](https://meta.stackoverflow.com/questions/255583/what-can-i-do-when-getting-we-are-no-longer-accepting-questions-answers-from-th) you need. – halfer Oct 23 '17 at 20:10
  • 1
    Thank you. I never did this before but this time I had no way to ask and I needed help. Anyway I myself won't edit the question if it had any answers as that seems completely nonsense. And thank you for pointing me out about it. :) – Mohammed Julfikar Ali Mahbub Oct 23 '17 at 20:15

1 Answers1

2

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
Community
  • 1
  • 1