Skip to content

ejectDrive and ejectDriveSync

xiao edited this page Mar 10, 2023 · 5 revisions

eject a drive by the specific drive letter.

usage

async:

fswin.ejectDrive(letter, method, callback);

sync:

var result = fswin.ejectDriveSync(letter, method);
  • letter is a letter of the alphabet to determine which drive to eject.
  • method is an integer between 0 to 2 to determine which method to use.
  1. use DeviceIoControl. this method is simple and works good with cdrom.
  2. use CM_Request_Device_EjectW or CM_Query_And_Remove_SubTreeW. this method works good with usb flash drive.
  3. use HotPlugEjectDevice from HotPlug.dll. this method works good with most media types but requires user interaction. and HotPlug.dll only exists in system32 folder which means it is not possible to use this method when you execute a 32bit node process in a 64bit windows.
  • callback is a function to make callback with. it takes only one argument result.
  • result is a boolean value to indicate whether the operation is succeeded.

example

var fswin = require('fswin');
var letter = 'e';

//sync
console.log(fswin.ejectDriveSync(letter, 1));

//async
fswin.ejectDrive(letter, 2, console.log);