Jump to content

How to create a program code that scans for usb sticks inserted in usb ports

Hello guys,

I'm searching for a way to check for a usb devices and execute a script when a new usb device is inserted.

Does someone know how to do that ?

Link to comment
Share on other sites

Link to post
Share on other sites

Specifiying your programming language would certainly help

I only see your reply if you @ me.

This reply/comment was generated by AI.

Link to comment
Share on other sites

Link to post
Share on other sites

Will also depend on OS. 

 

For Linux, you'd make a udev rule to trigger off of... If you want to trigger on any USB device being connected:

ACTION=="add", ATTRS{idVendor}=="****", ATTRS{idProduct}=="****", RUN+= "path_to_your_script"

For a specific device, you'd use lsusb to get the idVendor and idProduct values first.

 

For Windows, probably WMI event monitoring with class Win32_USBControllerDevice either with PS or the C++ WMI COM API... not sure. 

Link to comment
Share on other sites

Link to post
Share on other sites

For windows with C# it's a few line of code. Probably a couple more for other languages but it's accessible with any language that has access to WMI. What you do is actually bind to the windows events and detect volume change events when a volume is added it is returning a type 2.

 

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3217
{
    class Program
    {
        static void Main(string[] args)
        {
            var watcher = new ManagementEventWatcher();
            var eventQuery = new EventQuery("SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2");

            watcher.EventArrived += (s, e) =>
            {
                // this give you like "E:" or "H:"
                var drive = e.NewEvent.Properties["DriveName"].Value.ToString();

                if (File.Exists(drive + "\\myfile.script"))
                {
                    // execute the script  
                }
            };

            watcher.Query = eventQuery;
            watcher.Start();

            Console.ReadKey();
        }
    }
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

As others have stated; Welcome to the world of WMI. You will not like it, and there are multiple ways to do this depending on your criteria. For example, you need to do very different things if you only want USB devices, or if you only want "portable" devices, or if you want any devices. There are tons of different groups and stuff in WMI for a ton of different things, and nothing is the way you expect it to be. 

 

I recommend getting a debug utility to do manual queries to WMI to see whats available. Some already exist for this purpose.

 

GLHF.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×