用VBS检测U盘插入和弹出事件(二)

标签: , , ,

鉴于很多人反映之前写的那篇在XP下无效,做了一下修改。说是修改,其实是直接复制粘贴脚本专家的代码。

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colEvents = objWMIService.ExecNotificationQuery _
    ("Select * From __InstanceOperationEvent Within 10 Where " _
        & "TargetInstance isa 'Win32_LogicalDisk'")

Do While True
    Set objEvent = colEvents.NextEvent
    If objEvent.TargetInstance.DriveType = 2 Then 
        Select Case objEvent.Path_.Class
            Case "__InstanceCreationEvent"
                Wscript.Echo "Drive " & objEvent.TargetInstance.DeviceId & _
                    " has been added."
            Case "__InstanceDeletionEvent"
                Wscript.Echo "Drive " & objEvent.TargetInstance.DeviceId & _
                    " has been removed."
        End Select
    End If
Loop

参考链接:How Can I Determine When a Removable Drive Gets Connected?

赞赏

微信赞赏支付宝赞赏

随机文章:

  1. PTMaster,新的PT流量作弊工具?
  2. VBS实现UTF-8转Unicode(UTF-16)
  3. WMI中的Win32_PingStatus类
  4. IPv6免费传文件——uTorrent鲜为人知的功能
  5. Python Proxy,快速、轻量级的HTTP代理服务器

3 条评论 发表在“用VBS检测U盘插入和弹出事件(二)”上

  1. 非主流说道:

    拜读楼主大作,感谢分享好东西!

  2. Crlf说道:

    很实用,收藏了,谢谢分享 :)

  3. SIR007_001说道:

    请教一个问题,怎样用vbs代码实现象360小助手的弹出U盘功能!如能指教,不胜感激!

留下回复