C# Класс ZForge.Win32.RemovableDriveDetector

Detects insertion or removal of removable drives. Use it in 1 or 2 steps: 1) Create instance of this class in your project and add handlers for the DeviceArrived, DeviceRemoved and QueryRemove events. AND (if you do not want drive detector to creaate a hidden form)) 2) Override WndProc in your form and call DriveDetector's WndProc from there. If you do not want to do step 2, just use the DriveDetector constructor without arguments and it will create its own invisible form to receive messages from Windows.
Наследование: IDisposable
Показать файл Открыть проект Примеры использования класса

Открытые методы

Метод Описание
DisableQueryRemove ( ) : void

Unhooks any currently hooked drive so that the query remove message is not generated for it.

Dispose ( ) : void

Unregister and close the file we may have opened on the removable drive. Garbage collector will call this method.

EnableQueryRemove ( string fileOnDrive ) : bool

Hooks specified drive to receive a message when it is being removed. This can be achieved also by setting e.HookQueryRemove to true in your DeviceArrived event handler. By default DriveDetector will open the root directory of the flash drive to obtain notification handle from Windows (to learn when the drive is about to be removed).

RemovableDriveDetector ( ) : System

The easiest way to use DriveDetector. It will create hidden form for processing Windows messages about USB drives You do not need to override WndProc in your form.

RemovableDriveDetector ( Control control ) : System

Alternate constructor. Pass in your Form and DriveDetector will not create hidden form.

RemovableDriveDetector ( Control control, string FileToOpen ) : System

Consructs DriveDetector object setting also path to file which should be opened when registering for query remove.

WndProc ( Message &m ) : void

Message handler which must be called from client form. Processes Windows messages and calls event handlers.

Приватные методы

Метод Описание
DriveMaskToLetter ( int mask ) : char

Gets drive letter from a bit mask where bit 0 = A, bit 1 = B etc. There can actually be more than one drive in the mask but we just use the last one in this case.

Init ( Control control, string fileToOpen ) : void

init the DriveDetector object

RegisterForDeviceChange ( bool register, SafeFileHandle fileHandle ) : void

Registers to be notified when the volume is about to be removed This is requierd if you want to get the QUERY REMOVE messages

RegisterForDeviceChange ( string dirPath ) : void

New version which gets the handle automatically for specified directory Only for registering! Unregister with the old version of this function...

RegisterQuery ( string drive ) : void

Registers for receiving the query remove message for a given drive. We need to open a handle on that drive and register with this handle. Client can specify this file in mFileToOpen or we will open root directory of the drive

Описание методов

DisableQueryRemove() публичный Метод

Unhooks any currently hooked drive so that the query remove message is not generated for it.
public DisableQueryRemove ( ) : void
Результат void

Dispose() публичный Метод

Unregister and close the file we may have opened on the removable drive. Garbage collector will call this method.
public Dispose ( ) : void
Результат void

EnableQueryRemove() публичный Метод

Hooks specified drive to receive a message when it is being removed. This can be achieved also by setting e.HookQueryRemove to true in your DeviceArrived event handler. By default DriveDetector will open the root directory of the flash drive to obtain notification handle from Windows (to learn when the drive is about to be removed).
public EnableQueryRemove ( string fileOnDrive ) : bool
fileOnDrive string Drive letter or relative path to a file on the drive which should be /// used to get a handle - required for registering to receive query remove messages. /// If only drive letter is specified (e.g. "D:\\", root directory of the drive will be opened.
Результат bool

RemovableDriveDetector() публичный Метод

The easiest way to use DriveDetector. It will create hidden form for processing Windows messages about USB drives You do not need to override WndProc in your form.
public RemovableDriveDetector ( ) : System
Результат System

RemovableDriveDetector() публичный Метод

Alternate constructor. Pass in your Form and DriveDetector will not create hidden form.
public RemovableDriveDetector ( Control control ) : System
control Control object which will receive Windows messages. /// Pass "this" as this argument from your form class.
Результат System

RemovableDriveDetector() публичный Метод

Consructs DriveDetector object setting also path to file which should be opened when registering for query remove.
public RemovableDriveDetector ( Control control, string FileToOpen ) : System
control Control object which will receive Windows messages. /// Pass "this" as this argument from your form class.
FileToOpen string Optional. Name of a file on the removable drive which should be opened. /// If null, root directory of the drive will be opened. Opening a file is needed for us /// to be able to register for the query remove message. TIP: For files use relative path without drive letter. /// e.g. "SomeFolder\file_on_flash.txt"
Результат System

WndProc() публичный Метод

Message handler which must be called from client form. Processes Windows messages and calls event handlers.
public WndProc ( Message &m ) : void
m Message
Результат void