Geekpedia Programming Tutorials






Opening and closing the CD tray in .NET

This tutorial will teach you how to open and close the CD / DVD tray in C# by using the winmm DLL and unmanaged code calls with PInvoke.

On Friday, March 31st 2006 at 07:46 AM
By Andrew Pociu (View Profile)
*****   (Rated 5 with 15 votes)
Contextual Ads
More C# Resources
Advertisement
Microsoft .NET Framework doesn't provide a method that you can simply call for opening or closing the CD or DVD tray of the computer drives. However, that doesn't mean this can't be easily accomplished by using the mciSendString from the Windows API. The project attached to this tutorial is created in Visual Studio 2005, but the code works in Visual Studio 2003 aswell.

Start by creating a C# Windows application project. And drag two buttons to the form, btnOpen and btnClose, which will open and close the CD / DVD tray when clicked.
Now switch to code view, and add the following using directive, since we'll be working with unmanaged code:

using System.Runtime.InteropServices;


Inside the class (normally above the constructor), use the following two lines of code. They will call the winmm.dll DLL file into our C# code, using a mechanism known as PInvoke. This DLL (winmm.dll) is located in the System32 directory of Windows. The mciSendString function is used to send a command string to an MCI device, in this case the CD / DVD drive.

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


Now back at the form, double click the Open button (btnOpen) you added, to get the Click event handler. Inside the event handler, use the following code:

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


We are sending the set CDAudio door open command to the MCI device (the CD / DVD drive). You can run your application, click the button and wait for the tray to open.

Now you probably want to know how to close the tray too. So back at the form, double click the Close button (btnClose) and use the following function call to send the drive the set CDAudio door closed command:

mciSendString("set CDAudio door closed", null, 0, IntPtr.Zero);


After you compile and test this C# application, you will be able to open and close the drive's disc tray, and that with only a few lines of code.

I know what many of you are thinking now: "I have multiple CD / DVD drives and only one of them opens. How can I choose which one?".
The commands we sent above, only open / close the drive that's marked as the default drive for playing audio CDs. To open the other drives, we need to specify the letter of the drive that we wish to open. Let's say you have two DVD drives - H: and I: - like I do. Replace the code we wrote before in the button event handlers, with the following:

// Open Drive H:
mciSendString("open H: type CDAudio alias driveH", null, 0, IntPtr.Zero);
mciSendString("set driveH door open", null, 0, IntPtr.Zero);
// Open Drive I:
mciSendString("open I: type CDAudio alias driveI", null, 0, IntPtr.Zero);
mciSendString("set driveI door open", null, 0, IntPtr.Zero);


We first create an alias name for the H: drive named driveH, as a CDAudio type of drive, then in the second command, we open it by specifing the alias instead of CDAudio. The same thing is done for the I: drive.
The command for closing both drives is now obvious:

// Close Drive H:
mciSendString("open H: type CDAudio alias driveH", null, 0, IntPtr.Zero);
mciSendString("set driveH door closed", null, 0, IntPtr.Zero);
// Close Drive I:
mciSendString("open I: type CDAudio alias driveI", null, 0, IntPtr.Zero);
mciSendString("set driveI door closed", null, 0, IntPtr.Zero);


Only that to optimize this code a little bit, you can use the alias lines only once, by calling them in the form's Load event, and then just call the open door / close door commands in the event handlers of the buttons.
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by cherryl on Friday, July 28th 2006 at 06:28 AM

it is not compatible with VS.NET 2003...a got an error about missing namespace for the "StringBuilder" parameter

by Alton on Saturday, September 23rd 2006 at 07:40 AM

I added using System.Text; and it works.

by Darren on Wednesday, February 7th 2007 at 03:54 PM

It is possible to determine if the disc in the drive is a CD or DVD or DVD +R...
I can check the size of files on the disk but if less than say 700 Megs are burned on a DVD, it is not conclusive. I would prefer a method that returns a string \"DVD+R\" or a int code that I can map. Any ideas?

by Cameron on Tuesday, July 17th 2007 at 03:16 PM

System.IO.DriveInfo provides lots of, well... Drive Info! MSDN has lots of information on it at:

ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref8/html/T_System_IO_DriveInfo.htm

by ComoMolo on Friday, August 3rd 2007 at 12:41 AM

System.IO.DriveInfo doesn't seem to provide information about what type of disc is in the drive, only if the drive is loaded and ready or not.

I just want to check if the CD door is open or closed, so I would need just one simple command: if it's open, close it; if it's closed, open it.

Any suggestions?

by Flavio on Monday, December 3rd 2007 at 09:35 AM

SpaceName \"DriveInfo\" is only available since .NET Framework 2.0 (VB 2005 & above)

If you want to get info about drives you should do it the \"old way\" accesing Lib \"kernel32\"

Declare Function GetDriveType Lib \"kernel32\" Alias \"GetDriveTypeA\"(ByVal nDrive As String) As Integer

by Soumya on Friday, January 11th 2008 at 05:59 AM

Sorry. This code is not working on c#.net 2.0

by Paul on Friday, January 25th 2008 at 04:45 PM

this command works visual studio2003.

I rewrote the program so it open a dos command window.
at the command type:

set cdaudio door open



using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;


namespace MCICommandTestApplication
{
class Program
{
[DllImport(\"winmm.dll\")]
private static extern long mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback);
[DllImport(\"winmm.dll\")]
private static extern long mciGetDeviceID(string lpszDevice);
private static StringBuilder sBuffer = new StringBuilder(128);


static void Main(string[] args)
{

String mciString;
Console.WriteLine();
do
{
Console.Write(\"MCI command: \");
mciString = Console.ReadLine();
if (mciString.Length < 3)
{
break;
}
Console.WriteLine(mciString.Trim());
mciSendString(mciString.Trim(), sBuffer, sBuffer.Capacity, IntPtr.Zero);
Console.WriteLine(sBuffer);
}
while (true);
}

}
}




by Paul on Friday, January 25th 2008 at 04:51 PM

FYI
you can also do

sysinfo all quantity

and it will print the number of mci devices

etc...

by dharan on Thursday, May 29th 2008 at 05:56 AM

ya this code is working good
thanks

by Soumya on Saturday, January 31st 2009 at 03:30 PM

The code rocks...........

by Brad on Sunday, February 28th 2010 at 02:46 PM

Works great and allows me to have a keyboard shortcut to open the drive, cheers!!

by Mark on Wednesday, May 12th 2010 at 09:25 AM

Hi there,

the code is great, but i am trying to create lock system using the drive tray to slide a lock across the door, and i need to add another form that accepts a password before allowing me access to the open and close buttons. Anyone got any ideas?

Cheers


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs C# Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons