Windows Removable Removal Request Detection

I have a Windows service written in C # with the .NET 2.0 platform that uses FileSystemWatcher to monitor specific directories for changes. Some of these directories may be on removable media, such as a USB drive. When a new disk is connected to the system, I receive a notification through a WMI request and can configure a new FileSystemWatcher. It all works well.

The difficulty is that I want the user to be able to eject the disk using the Windows Safe Hardware Removal application, but Windows reports that the disk is busy because I am tracking it through FileSystemWatcher and tells the user that the disk cannot be deleted.

So my question is, how can my Windows service know when a user requests a surge so that I can remove the file system monitor and allow the request to succeed?

+2
source share
1 answer

You need to register to receive the broadcast device removal request event (DBT_DEVICEQUERYREMOVE) through the Windows RegisterDeviceNotification API, as described here .

As far as I know, there is no .NET Framework class that wraps this functionality, so you have to do it with p / Invoke.

+2
source

Source: https://habr.com/ru/post/1347993/


All Articles