如何在不使用 Ruby 和 WMI 轮询的情况下检测 USB 插入? [英] How could I detect USB insertion without polling using Ruby and WMI?
问题描述
我阅读了以下文章:使用 Ruby &WMI 检测 USB 驱动器
然而,这种方法需要我在循环内继续轮询.是否可以注册并在插入/弹出 USB 时通知我的脚本?
However, this method would require me to keep polling inside a loop. Is it possible to register and have my script be notified when USB is inserted/ejected ?
我正在寻找 Windows XP 解决方案.
I am looking for a Windows XP solution.
推荐答案
我对 Ruby 的帮助不大,但 WMI 也支持受监控的事件.存在一个名为 Win32_DeviceChangeEvent 的外部事件.
I can't help you much with Ruby, but WMI also supports monitored events. There exists an extrinsic event called Win32_DeviceChangeEvent.
这是一个简单的 PowerShell 代码来使用它:
Here is a simple PowerShell code to use it:
$query = "SELECT * FROM Win32_DeviceChangeEvent WHERE EventType=2"
Register-WMIEvent -Query $query -Action { Write-Host "A device has been inserted"}
每次插入设备时都会调用给 Action 参数的代码.我不知道在 Ruby 中处理这样的查询.
The code given into Action parameter is called each time a device is inserted. I don't know to handle such a query in Ruby.
这篇关于如何在不使用 Ruby 和 WMI 轮询的情况下检测 USB 插入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!