.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Visual Studio >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

detect insertion and removal of USB Drive C#

Posted By:Sagar Pardeshi       Posted Date: November 29, 2013    Points: 40    Category: Visual Studio    URL: http://www.dotnetspark.com  

I have located the following code and trying to change this so I know if the USB device has been inserted or removed.
 

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;

namespace WMIUSBConsolApplication
{

    class Program
    {

        static void Main(string[] args)
        {

            AddInsetUSBHandler();
            AddRemoveUSBHandler();
            for (; ; ) ;

        }

        static ManagementEventWatcher w = null;

        public static void AddRemoveUSBHandler()
        {

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

            try
            {

                q = new WqlEventQuery();
                q.EventClassName = "__InstanceDeletionEvent";
                q.WithinInterval = new TimeSpan(0, 0, 3);
                q.Condition = @"TargetInstance ISA 'Win32_USBHub'";
                w = new ManagementEventWatcher(scope, q);
                w.EventArrived += new EventArrivedEventHandler(USBRemoved);
                w.Start();

            }

            catch (Exception e)
            {

                Console.WriteLine(e.Message);
                if (w != null)
                    w.Stop();

            }

        }

        static void AddInsetUSBHandler()
        {

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

            try
            {

                q = new WqlEventQuery();
                q.EventClassName = "__InstanceCreationEvent";
                q.WithinInterval = new TimeSpan(0, 0, 3);
                q.Condition = @"TargetInstance ISA 'Win32_USBHub'";
                w = new ManagementEventWatcher(scope, q);
                w.EventArrived += new EventArrivedEventHandler(USBAdded);
                w.Start();

            }
            catch (Exception e)
            {

                Console.WriteLine(e.Message);
                if (w != null)
                    w.Stop();

            }

        }

        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");

        }
    }

}

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend