Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Using Ruby & WMI to Detect a USB Drive (See related posts)

From the Ruby on Windows blog.

How to use Windows Management Instrumentation (WMI) to determine if a "USB Mass Storage Device" is inserted.
require 'win32ole'

wmi = WIN32OLE.connect("winmgmts://")

devices = wmi.ExecQuery("Select * From Win32_USBControllerDevice")
for device in devices do
    device_name = device.Dependent.gsub('"', '').split('=')[1]
    usb_devices = wmi.ExecQuery("Select * From Win32_PnPEntity Where DeviceID = '#{device_name}'")
    for usb_device in usb_devices do
        puts usb_device.Description
        if usb_device.Description == 'USB Mass Storage Device'
            # DO SOMETHING HERE
        end
    end
end

Further details can be found here.


You need to create an account or log in to post comments to this site.


Click here to browse all 5137 code snippets

Related Posts