c# - 使用 Windows 服务和 Vb.Net 检测 USB 驱动器的插入和移除

标签 c# vb.net wmi wndproc managementeventwatcher

我希望在我的应用程序中检测 USB 驱动器是否已插入或拔出。

我在谷歌上搜索了很多相关内容,实际上找到了很多答案,但没有一个完全按照我想要的方式工作。我发现一个工作完美,并在插入或拔出驱动器时给出消息,但使用 WndProc 这是一个非常长的过程,很难理解,特别是对于我来说,我对此了解为零,但这不是主要问题。我发现 WndProc 的主要问题是它无法执行我想要执行的某些功能,而 WMI 可以执行这些功能。我还找到了WMI解决方案,它可以在驱动器插入时检测驱动器,但它无法检测设备何时拔出 em> 这对我的程序非常重要。我找到了另一个似乎有效的解决方案,但它是在 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# - stackoverflow.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.

Win32_VolumeChangeEvent class

  • 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

关于c# - 使用 Windows 服务和 Vb.Net 检测 USB 驱动器的插入和移除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46759004/

相关文章:

c# - 将图像转换为结构

c# - 无法读取配置部分 'system.serviceModel',因为它缺少部分声明

vb.net - LINQ to SQL 并使用 OR 子句连接两个表

vb.net - ASP.NET 身份代码从 C# 转换为 VB.NET

c# - 升级到 Windows 10 后 WMI 不工作

c# - WMI 报告非唯一处理器 ID、硬盘驱动器 ID 和 MAC 地址

networking - 在 Windows 上以编程方式检测连接的网络速度

c# - 来自 App 常量的流畅验证自定义检查

c# - 处理嵌套代码条件的更好模式?

c# - 如何找到创建对象的程序集?