Programatically ejecting and retracting the CD drive in vb.net or c#

Cyclone picture Cyclone · Sep 19, 2009 · Viewed 13.1k times · Source

Is there any way to do so? I know its possible to programmatically eject/retract the cd drive SOMEHOW, cause Roxio does that when it prompts me to insert a disk.

Either c# or vb.net is preferable, but c and c++ are okay too as a last resort.

I am nearly positive there is some way to do this, I just don't know the methods to call.

I do understand this is a somewhat unusual request, as Google yielded absolutely nothing when I searched for the methods...

Answer

Robert Harvey picture Robert Harvey · Sep 19, 2009
using System.Runtime.InteropServices;

[DllImport("winmm.dll")]
static extern Int32 mciSendString(String command, StringBuilder buffer, Int32 bufferSize, IntPtr hwndCallback);

// To open the door
mciSendString("set CDAudio door open", null, 0, IntPtr.Zero);

// To close the door
mciSendString("set CDAudio door closed", null, 0, IntPtr.Zero);

http://www.geekpedia.com/tutorial174_Opening-and-closing-the-CD-tray-in-.NET.html