0

I want to detect when a removal media is inserted or removed from the PC and I have read many threads of how to do it.

Reading other threads and pages gives essential two methods

  1. Override WinProc method
  2. Make SQL query

There is a FileSystemWatcher class, is there reallý not a "DriveSystemWatcher" or something ?

Also, if I try the "SQL" approach it can not compile the program becosue it can not resolve "ManagementEventWatcher", even if I do

using System;

using System.Management;

Help would be appreciated

Regards Stefan.

Tim Lloyd
  • 37,954
  • 10
  • 100
  • 130
Stefan Olsson
  • 617
  • 3
  • 16
  • 32
  • Have you at least added a reference to `System.Management.dll` to your project if you wish to use classes from `System.Management`? – Tim Lloyd Aug 15 '11 at 08:58
  • I'm not sure what you mean by "override WinProc method" *or* how you would use Sql to detect a media insertion. Regardless, this seems to be a dupe... – Dan Puzey Aug 15 '11 at 08:59
  • possible duplicate of [Detecting Eject/Insert of Removeable Media](http://stackoverflow.com/questions/1662794/detecting-eject-insert-of-removeable-media) – Dan Puzey Aug 15 '11 at 08:59
  • @Dan I think the OP means "querying WMI" when Sql is mentioned??? – Tim Lloyd Aug 15 '11 at 09:00
  • The "override WinProc" is when trapping low level windows messages manually The "SQL thing" is @"TargetInstance ISA 'Win32_LogicalDisk' and TargetInstance.DriveType = 5"; Looks to me some simplified sql – Stefan Olsson Aug 15 '11 at 10:29

1 Answers1

1

In order to use classes from System.Management (so that you can query WMI) you need to add a reference to System.Management.dll to your project.

The following describes an approach using WMI when you've done this:

Detecting Eject/Insert of Removeable Media

Community
  • 1
  • 1
Tim Lloyd
  • 37,954
  • 10
  • 100
  • 130
  • Yes, I had forgott to incldue "System.Management.dll", thanks for the tip. – Stefan Olsson Aug 15 '11 at 10:25
  • However when I implement the code in "Detecting Eject/Insert of Removeable Media" it does not trap the usb memory stick, even if I do "TargetInstance ISA 'Win32_LogicalDisk' and TargetInstance.DriveType = 5 or TargetInstance.DriveType = 2" – Stefan Olsson Aug 15 '11 at 10:31
  • @user765903 HTH - You need to add your comment to that question. – Tim Lloyd Aug 15 '11 at 10:35