4

How do you programmatically eject(safely remove) an USB mass storage device in Windows (XP)?

Keng
  • 52,011
  • 32
  • 81
  • 111
botismarius
  • 2,977
  • 2
  • 30
  • 29

3 Answers3

2

First download de code from http://www.codeproject.com/Articles/13530/Eject-USB-disks-using-C

Then import the classes of the folder "Library" to your project

And put this code into your click button.

private void btnExpulsar_Click(object sender, RoutedEventArgs e)
        {
            //Expulsa todas las unidades
            VolumeDeviceClass volumeDeviceClass = new VolumeDeviceClass();  //Enlista las unidades
            foreach (var item in volumeDeviceClass.Devices.ToList())
            {
                if (item.IsUsb)//Verifica que sean unidades USB
                {
                    item.Eject(true); //Expulsa las unidades
                }
            }
        }   
Víctor
  • 51
  • 4
2

In autoit, you have a script which does just that.

It basically comes from this Microsoft article and uses kernel32.dll DeviceIoControl function

Of course, this question appears to be a duplicate of Safe remove USB-Drive using Win32 API?, which gives other solutions

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

You could spawn a process and use a command line tool. This would also work from other languages such as java where calling the Win32 api is harder.

johnstok
  • 96,212
  • 12
  • 54
  • 76
  • Not quite what I was looking for, but thanks anyway :). Maybe their's license do not forbids disassembly their code... – botismarius Oct 15 '08 at 20:23