locked
I have registered to an event to detect usb insertion but sometimes its detecting the usb disk on key on first time sometimes i need to insert it again to detect it: RRS feed

  • Question

  • This is the code in the constructor where i register the event:

     

    scope = new ManagementScope("root\\CIMV2");
                scope.Options.EnablePrivileges = true;

    try 

                 { 
                     WqlEventQuery query = new WqlEventQuery(); 
                     query.EventClassName = "__InstanceCreationEvent"; 
                     query.WithinInterval = new TimeSpan(0, 0, 1); 
                     query.Condition = @"TargetInstance ISA 'Win32_USBControllerdevice'"; 
                       
                     watcher = new ManagementEventWatcher(scope, query); 
                     watcher.EventArrived += new EventArrivedEventHandler(WaitForUSBChangeEvent); 
                   listBox1.Items.Add("Insert a usb device and watch what happens");
                     watcher.Start();
       
                 } 
                 catch (ManagementException ) 

                 {             
                 }

     

    And this is the event function:

     

     

    public void WaitForUSBChangeEvent(object sender, EventArrivedEventArgs e) 
             {
                
                 watcher.Stop();
                
                 this.Invoke((MethodInvoker)delegate
                 {
                     ManagementObjectSearcher mosDiskst = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
                     // Loop through each object (disk) retrieved by WMI
                  
                         foreach (ManagementObject moDiskete in mosDiskst.Get())
                         {
                             if (comboBox1.Items.Contains(moDiskete["Model"]) == false)
                             {
                                 // Add the HDD to the list (use the Model field as the item's caption)
                                 comboBox1.Items.Add(moDiskete["Model"].ToString());
                             }

                         }
                        
                 
                 });
               
                 watcher.Start();
                
               
            }

     

    So two things:

     

    One thing is when i insert my Verbatim 8GB disk on key to the usb sometimes its detecting it on first time and show it in the comboBox and sometimes it dosent show it on first time so i have to take it out and insert it again. Any ideas why its not detecting it on the first time every time i try to insert it?

     

    Second thing is im using another event for removal usb devices so if i remove any device it should do something. But i want to use the same only device im using now as i show here for detect devices insertion and removal. Somehow i need to refresh the list in the comboBox1 each time an event is coming. Refresh didnt help maybe something else? How can i make that the list will be "refresh" each time a new event is coming?


    danieli
    Monday, August 30, 2010 3:35 PM

Answers

  • Hi chocolade,

     

    Welcome to MSDN forums!

     I am interested in helping.

    I write some code, I’d like to share with you.

    ManagementScope scope;

            ManagementEventWatcher watcher;

            ManagementEventWatcher watcher1;

            private void button1_Click(object sender, EventArgs e)

            {

                scope = new ManagementScope("root\\CIMV2");

                scope.Options.EnablePrivileges = true;

                try

                {

                    WqlEventQuery query = new WqlEventQuery();

                    query.EventClassName = "__InstanceCreationEvent";

                    query.WithinInterval = new TimeSpan(0, 0, 1);

                    query.Condition = @"TargetInstance ISA 'Win32_USBControllerdevice'";

                    watcher = new ManagementEventWatcher(scope, query);

                    watcher.EventArrived += new EventArrivedEventHandler(WaitForUSBChangeEvent);

                    listBox1.Items.Add("Insert a usb device and watch what happens");

                    watcher.Start();

                }

                catch (ManagementException)

                {

                }

            }

            public void WaitForUSBChangeEvent(object sender, EventArrivedEventArgs e)

            {

                (sender as ManagementEventWatcher).Stop();

               // watcher.Stop();

                this.Invoke((MethodInvoker)delegate

                {

                    ManagementObjectSearcher mosDiskst = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");

                    // Loop through each object (disk) retrieved by WMI

                    this.comboBox1.Items.Clear();

                   //

                    foreach (ManagementObject moDiskete in mosDiskst.Get())

                    {

                        if (!comboBox1.Items.Contains(moDiskete["Model"]))

                        {

                            // Add the HDD to the list (use the Model field as the item's caption)

                            comboBox1.Items.Add(moDiskete["Model"].ToString());

                        }

                    }

                    this.comboBox1.SelectedIndex = 0;

                });

                (sender as ManagementEventWatcher).Start();

            }

            //remove the usb

            private void button2_Click(object sender, EventArgs e)

            {

                scope = new ManagementScope("root\\CIMV2");

                scope.Options.EnablePrivileges = true;

                try

                {

                    WqlEventQuery query = new WqlEventQuery();

                    query.EventClassName = "__InstanceDeletionEvent";// here is different

                    query.WithinInterval = new TimeSpan(0, 0, 1);

                    query.Condition = @"TargetInstance ISA 'Win32_USBControllerdevice'";

     

                    watcher1 = new ManagementEventWatcher(scope, query);

                    watcher1.EventArrived += new EventArrivedEventHandler(WaitForUSBChangeEvent);

                    listBox1.Items.Add("Insert a usb device and watch what happens");

                    watcher1.Start();

     

                }

                catch (ManagementException)

                {

                }

            }

     it works ok!

    Hope these helps, if you have any problems, please feel free to let me know.

     

    Best Regards,

    Alan Chen

    ________________________________________

    Please remember to mark the replies as answers if they help and unmark them if they provide no help

    • Marked as answer by chocolade Thursday, September 2, 2010 7:55 AM
    Thursday, September 2, 2010 3:23 AM

All replies

  • I noticed now that if im chaniging:  ManagementObjectSearcher mosDiskst = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
    To :  ManagementObjectSearcher mosDiskst = new ManagementObjectSearcher("SELECT * FROM Win32_CDROMdrive");

    Then when i insert my usb cdrom drive its allways detecting it on first time i insert it.

    But when its ManagementObjectSearcher mosDiskst = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
    And i insert my Verbatim 8gb disk on key in the usb sometimes its detecting it on first time and sometimes i need to remove it and insert it again to be detected.

     

    Maybe there is some Win32_ for disk on key? I didnt find anything.

     

    Thanks for help.

     


    danieli
    Monday, August 30, 2010 3:42 PM
  • Hi chocolade,

     

    Welcome to MSDN forums!

     I am interested in helping.

    I write some code, I’d like to share with you.

    ManagementScope scope;

            ManagementEventWatcher watcher;

            ManagementEventWatcher watcher1;

            private void button1_Click(object sender, EventArgs e)

            {

                scope = new ManagementScope("root\\CIMV2");

                scope.Options.EnablePrivileges = true;

                try

                {

                    WqlEventQuery query = new WqlEventQuery();

                    query.EventClassName = "__InstanceCreationEvent";

                    query.WithinInterval = new TimeSpan(0, 0, 1);

                    query.Condition = @"TargetInstance ISA 'Win32_USBControllerdevice'";

                    watcher = new ManagementEventWatcher(scope, query);

                    watcher.EventArrived += new EventArrivedEventHandler(WaitForUSBChangeEvent);

                    listBox1.Items.Add("Insert a usb device and watch what happens");

                    watcher.Start();

                }

                catch (ManagementException)

                {

                }

            }

            public void WaitForUSBChangeEvent(object sender, EventArrivedEventArgs e)

            {

                (sender as ManagementEventWatcher).Stop();

               // watcher.Stop();

                this.Invoke((MethodInvoker)delegate

                {

                    ManagementObjectSearcher mosDiskst = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");

                    // Loop through each object (disk) retrieved by WMI

                    this.comboBox1.Items.Clear();

                   //

                    foreach (ManagementObject moDiskete in mosDiskst.Get())

                    {

                        if (!comboBox1.Items.Contains(moDiskete["Model"]))

                        {

                            // Add the HDD to the list (use the Model field as the item's caption)

                            comboBox1.Items.Add(moDiskete["Model"].ToString());

                        }

                    }

                    this.comboBox1.SelectedIndex = 0;

                });

                (sender as ManagementEventWatcher).Start();

            }

            //remove the usb

            private void button2_Click(object sender, EventArgs e)

            {

                scope = new ManagementScope("root\\CIMV2");

                scope.Options.EnablePrivileges = true;

                try

                {

                    WqlEventQuery query = new WqlEventQuery();

                    query.EventClassName = "__InstanceDeletionEvent";// here is different

                    query.WithinInterval = new TimeSpan(0, 0, 1);

                    query.Condition = @"TargetInstance ISA 'Win32_USBControllerdevice'";

     

                    watcher1 = new ManagementEventWatcher(scope, query);

                    watcher1.EventArrived += new EventArrivedEventHandler(WaitForUSBChangeEvent);

                    listBox1.Items.Add("Insert a usb device and watch what happens");

                    watcher1.Start();

     

                }

                catch (ManagementException)

                {

                }

            }

     it works ok!

    Hope these helps, if you have any problems, please feel free to let me know.

     

    Best Regards,

    Alan Chen

    ________________________________________

    Please remember to mark the replies as answers if they help and unmark them if they provide no help

    • Marked as answer by chocolade Thursday, September 2, 2010 7:55 AM
    Thursday, September 2, 2010 3:23 AM
  • Hi, Alan chen.  Can you help me? I use above codes which can  detect usb insertion. But I don't know how to get files inside it. Can you help me to solve this problem? Thanks a lot. 
    Tuesday, July 24, 2012 7:59 AM
  • This helps a lot. Thanks

    Thursday, June 27, 2013 8:47 AM