managementeventwatcher - How do I get newly inserted USB drive letter in c#? -


i wrote c# program find newly inserted usb drive , drive letter. when run program got insertion event , couldn't drive letter. can suggest me idea this?

code

static void main(string[] args) {     managementeventwatcher mwe_creation; //object creation 'managementeventwatcher' class used listen temporary system event notofications based on specific query.      wqleventquery q_creation = new wqleventquery(); //represents wmi(windows management instrumentation) event query in wql format more information goto www.en.wikipedia.org/wiki/wql     q_creation.eventclassname = "__instancecreationevent";// sets eventclass query     q_creation.withininterval = new timespan(0, 0, 2);    // setting time interval event check(here, 2 seconds)     q_creation.condition = @"targetinstance isa 'win32_diskdrivetodiskpartition'"; //sets kind of event  notified     mwe_creation = new managementeventwatcher(q_creation); //initializing new instance     mwe_creation.eventarrived += new eventarrivedeventhandler(usbeventarrived_creation);//calling 'usbeventarrived_creation' method when usb storage plug-in event occured     mwe_creation.start(); // starting listen system events based on given query     while (true) ;  } static void usbeventarrived_creation(object sender, eventarrivedeventargs e){      console.writeline("usb plugged in!");     managementbaseobject instance = (managementbaseobject)e.newevent["targetinstance"];     foreach (var property in instance.properties)     {          if (property.name == "name")             console.writeline(property.name + " = " + property.value);     }  } 

you may working hard recreate pre-existing soultion. here public-licensed project made stephen mcohn provides interface need , appears documented:

http://www.codeproject.com/articles/63878/enumerate-and-auto-detect-usb-drives

here how filtered usb drives

new managementobjectsearcher(         "select deviceid, model win32_diskdrive " +          "where interfacetype='usb'").get()) 

here how specific drive letter recovered

getting specific drive letter accomplished using associators win32_logicaldisk can give device id (e.g. drive letter).

microsoft provided vb code example can port if don't want import stephen's whole module:

computername = "." set wmiservices  = getobject ( _     "winmgmts:{impersonationlevel=impersonate}!//" & computername) ' physical disk drive set wmidiskdrives =  wmiservices.execquery ( "select caption, deviceid win32_diskdrive")  each wmidiskdrive in wmidiskdrives     wscript.echo "disk drive caption: " & wmidiskdrive.caption & vbnewline & "deviceid: " & " (" & wmidiskdrive.deviceid & ")"      'use disk drive device id     ' find associated partition     query = "associators of {win32_diskdrive.deviceid='" _         & wmidiskdrive.deviceid & "'} assocclass = win32_diskdrivetodiskpartition"         set wmidiskpartitions = wmiservices.execquery(query)      each wmidiskpartition in wmidiskpartitions         'use partition device id find logical disk         set wmilogicaldisks = wmiservices.execquery _             ("associators of {win32_diskpartition.deviceid='" _              & wmidiskpartition.deviceid & "'} assocclass = win32_logicaldisktopartition")           each wmilogicaldisk in wmilogicaldisks             wscript.echo "drive letter associated" _                 & " disk drive = " _                  & wmidiskdrive.caption _                 & wmidiskdrive.deviceid _                 & vbnewline & " partition = " _                 & wmidiskpartition.deviceid _                 & vbnewline & " " _                 & wmilogicaldisk.deviceid         next           next next 

Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -