使用 windows 服务和 Vb.Net 检测 USB 驱动器插入和移除
Detecting USB drive insertion and removal using windows service and Vb.Net
我想在我的应用程序中检测 USB 驱动器是否插入或拔出。
我在谷歌上搜索了很多,实际上找到了很多答案,但 none 完全按照我想要的方式工作。我找到了一个可以完美运行并在插入或拔出驱动器时发出消息但使用 WndProc 的过程,这是一个非常长的过程,特别是对于知识为零的我来说很难理解关于它,但这不是主要问题。我发现 WndProc 的主要问题是它不能执行某些我想执行的功能,而 WMI 可以执行这些功能。我还找到了 WMI 解决方案,它可以在驱动器 插入 时检测到驱动器,但 它无法检测设备何时被拔出,这对我的程序非常重要。我找到了另一个似乎有效的解决方案,但它在 C# 代码中,我试图将它转换为 VB.Net 但是当我进入该 C# 代码的代码行 4 时未能这样做(我将在稍后添加下面的问题)。
部分帮助我的解决方案链接:
- 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# - whosebug.com
Seems to work but failed to convert it to Vb.Net
我认为可能有效的 C# 代码:
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();
我找到了解决方案:)
Ref.
- Configuration Changed (1)
- Device Arrival (2)
- Device Removal (3)
- Docking (4)
代码:
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
我想在我的应用程序中检测 USB 驱动器是否插入或拔出。
我在谷歌上搜索了很多,实际上找到了很多答案,但 none 完全按照我想要的方式工作。我找到了一个可以完美运行并在插入或拔出驱动器时发出消息但使用 WndProc 的过程,这是一个非常长的过程,特别是对于知识为零的我来说很难理解关于它,但这不是主要问题。我发现 WndProc 的主要问题是它不能执行某些我想执行的功能,而 WMI 可以执行这些功能。我还找到了 WMI 解决方案,它可以在驱动器 插入 时检测到驱动器,但 它无法检测设备何时被拔出,这对我的程序非常重要。我找到了另一个似乎有效的解决方案,但它在 C# 代码中,我试图将它转换为 VB.Net 但是当我进入该 C# 代码的代码行 4 时未能这样做(我将在稍后添加下面的问题)。
部分帮助我的解决方案链接:
- 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# - whosebug.com
Seems to work but failed to convert it to Vb.Net
我认为可能有效的 C# 代码:
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();
我找到了解决方案:)
Ref.
- Configuration Changed (1)
- Device Arrival (2)
- Device Removal (3)
- Docking (4)
代码:
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