12 Tem

C# Open And Close CD Drive Tray

This example shows how to open and close the tray of the CD drive. First we will need to define the following function that will be opening the disk tray:

        [DllImport("winmm.dll", EntryPoint = "mciSendString")]
        public static extern int mciSendString(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback);

Usage:

            //To open
            int result = mciSendString("set cdaudio door open", null, 0, 0);

            //To close
            result = mciSendString("set cdaudio door closed", null, 0, 0);