Help me Get Name Of USB Instance Inserted/Removed. Code Below


kindly me improve code posted solution display messages : 
 
"usb serial (com4)" plugged.
 
"usb serial (com4)" unplugged.

using system.management;

namespace usbdection
{
    class program
    {
        static managementeventwatcher watchingobect = null;
        static wqleventquery watcherquery;
        static managementscope scope;
        static void main(string[] args)
        {
            scope = new managementscope("root\\cimv2");
            scope.options.enableprivileges = true;

            addinsetusbhandler();
            addremoveusbhandler();

            (; ; ) ;
        }

        public static void addremoveusbhandler()
        {

            try
            {
                usbwatchersetup("__instancedeletionevent");
                watchingobect.eventarrived += new eventarrivedeventhandler(usbremoved);
                watchingobect.start();

            }

            catch (exception e)
            {

                console.writeline(e.message);
                if (watchingobect != null)
                    watchingobect.stop();

            }

        }

        static void addinsetusbhandler()
        {

            try
            {
                usbwatchersetup("__instancecreationevent");
                watchingobect.eventarrived += new eventarrivedeventhandler(usbadded);
                watchingobect.start();

            }
            catch (exception e)
            {

                console.writeline(e.message);
                if (watchingobect != null)
                    watchingobect.stop();

            }

        }

        private static void usbwatchersetup(string eventtype)
        {

            watcherquery = new wqleventquery();
            watcherquery.eventclassname = eventtype;
            watcherquery.withininterval = new timespan(0, 0, 2);
            watcherquery.condition = @"targetinstance isa 'win32_usbcontrollerdevice'";
            watchingobect = new managementeventwatcher(scope, watcherquery);
        }

        public static void usbadded(object sender, eventargs e)
        {

            console.writeline("a usb device inserted");

        }

        public static void usbremoved(object sender, eventargs e)
        {

            console.writeline("a usb device removed");

        }
    }

}

            

hi yaddly,

as far know, can use wmi tasks.

wmi tasks computer hardware obtain information presence, state, or properties of hardware components.

you can check in msdn wmi tasks: computer hardware

you may check connected usb devices

managementscope sc =             new managementscope(@"\\yourcomputername\root\cimv2");          objectquery query =             new objectquery("select * win32_usbhub");          managementobjectsearcher searcher = new managementobjectsearcher(sc, query);         managementobjectcollection result = searcher.get();          foreach (managementobject obj in result)         {             if (obj["description"] != null) console.writeline("description:\t" + obj["description"].tostring());             if (obj["deviceid"] != null) console.writeline("deviceid:\t" + obj["deviceid"].tostring());             if (obj["pnpdeviceid"] != null) console.writeline("pnpdeviceid:\t" + obj["pnpdeviceid"].tostring());         }
more information, please click here

there article in code project , please have look

detecting usb drive removal in c# program

best regards,

kristin


we trying better understand customer views on social support experience, participation in interview project appreciated if have time. helping make community forums great place.
click here participate survey.



Visual Studio Languages  ,  .NET Framework  >  Visual C#



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

job syspolicy_purge_history job fail in sqlserver 2008

Trying to register with public marketplace error with 'Get-AzureStackStampInformation'