Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I detect when a removable disk is inserted using C#?

Tags:

c#

windows

wpf

I'm just concerned about Windows, so there's no need to go into esoterica about Mono compatibility or anything like that.

I should also add that the app that I'm writing is WPF, and I'd prefer to avoid taking a dependency on System.Windows.Forms if at all possible.

like image 470
Amanda Mitchell Avatar asked Nov 07 '08 04:11

Amanda Mitchell


People also ask

Why a pendrive is showing as a removable disk?

This issue might be because the drivers might not be installed or pen drive might have been corrupted. Method 1: Run hardware and devices troubleshooter. Connect the pen drive to laptop.


2 Answers

Give this a shot...

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

namespace WMITestConsolApplication
{

    class Program
    {

        static void Main(string[] args)
        {

            AddInsertUSBHandler();
            AddRemoveUSBHandler();
            while (true) {
            }

        }

        static ManagementEventWatcher w = null;

        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_USBControllerdevice'";
                w = new ManagementEventWatcher(scope, q);
                w.EventArrived += USBRemoved;

                w.Start();
            }
            catch (Exception e) {


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

                }
            }

        }

        static void AddInsertUSBHandler()
        {

            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_USBControllerdevice'";
                w = new ManagementEventWatcher(scope, q);
                w.EventArrived += USBInserted;

                w.Start();
            }
            catch (Exception e) {

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

                }
            }

        }

        static void USBInserted(object sender, EventArgs e)
        {

            Console.WriteLine("A USB device inserted");

        }

        static void USBRemoved(object sender, EventArgs e)
        {

            Console.WriteLine("A USB device removed");

        }
    }

}
like image 154
Josh Stodola Avatar answered Oct 21 '22 00:10

Josh Stodola


There are much less cumbersome ways of doing this than using WMI polling - just capture WM_DEVICECHANGE:

http://msdn.microsoft.com/en-us/library/aa363215.aspx

like image 42
Ana Betts Avatar answered Oct 21 '22 00:10

Ana Betts