Exemplo n.º 1
0
        public override void Eject()
        {
            IntPtr fileHandle = IntPtr.Zero;

            try {
                // Create an handle to the drive
                fileHandle = WindowsAPI.CreateFile(this.CreateDeviceIOPath,
                                                   WindowsAPI.GENERICREAD, 0, IntPtr.Zero,
                                                   WindowsAPI.OPENEXISTING, 0, IntPtr.Zero);

                if ((int)fileHandle != WindowsAPI.INVALID_HANDLE)
                {
                    // Eject the disk
                    uint returnedBytes;
                    WindowsAPI.DeviceIoControl(fileHandle, WindowsAPI.IOCTL_STORAGE_EJECT_MEDIA,
                                               IntPtr.Zero, 0,
                                               IntPtr.Zero, 0,
                                               out returnedBytes,
                                               IntPtr.Zero);
                }
            } catch {
                throw new Exception(Marshal.GetLastWin32Error().ToString());
            } finally {
                // Close Drive Handle
                WindowsAPI.CloseHandle(fileHandle);
                fileHandle = IntPtr.Zero;
            }
        }