Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

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 23 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

by Mohsen on Tuesday, November 23rd 2010 at 05:47 AM

Open/Close CD

by Jacob on Tuesday, November 30th 2010 at 03:19 PM

I have noticed that this seems to work only on IDE CD/DVD drives. Drives that are using SATA will not open. Is there another way to make both work?

by how to get a credit card with no credit on Monday, December 12th 2011 at 05:30 AM

Use System.Windows.Forms.Cursor.Position to get the cursor position in screen coordinates.I admire the important information you offer within your content. I'll bookmark your web site and have my kids examine up the following typically.

by GenF20 Plus on Tuesday, December 20th 2011 at 03:35 AM

Thanks a lot for providing individuals with a very wonderful possiblity to discover important secrets from this web site. It's always so enjoyable and as well , stuffed with a great time for me personally and my office colleagues to search your website at the least three times in a week to read the new stuff you have. And lastly, I'm so always fascinated with all the magnificent suggestions you give. Certain 4 tips in this article are particularly the very best we've ever had.

by Human Growth Hormone on Tuesday, December 20th 2011 at 03:40 AM

Thanks a lot for providing individuals with a very wonderful possiblity to discover important secrets from this web site. It's always so enjoyable and as well , stuffed with a great time for me personally and my office colleagues to search your website at the least three times in a week to read the new stuff you have. And lastly, I'm so always fascinated with all the magnificent suggestions you give. Certain 4 tips in this article are particularly the very best we've ever had.

by Best Diet Pills on Tuesday, December 20th 2011 at 03:53 AM

Thanks a lot for providing individuals with a very wonderful possiblity to discover important secrets from this web site. It's always so enjoyable and as well , stuffed with a great time for me personally and my office colleagues to search your website at the least three times in a week to read the new stuff you have. And lastly, I'm so always fascinated with all the magnificent suggestions you give. Certain 4 tips in this article are particularly the very best we've ever had.

by Teeth Whitening on Tuesday, December 20th 2011 at 03:56 AM

Thanks a lot for providing individuals with a very wonderful possiblity to discover important secrets from this web site. It's always so enjoyable and as well , stuffed with a great time for me personally and my office colleagues to search your website at the least three times in a week to read the new stuff you have. And lastly, I'm so always fascinated with all the magnificent suggestions you give. Certain 4 tips in this article are particularly the very best we've ever had.

by Pure Acai Berry Max on Tuesday, December 20th 2011 at 03:59 AM

Thanks a lot for providing individuals with a very wonderful possiblity to discover important secrets from this web site. It's always so enjoyable and as well , stuffed with a great time for me personally and my office colleagues to search your website at the least three times in a week to read the new stuff you have. And lastly, I'm so always fascinated with all the magnificent suggestions you give. Certain 4 tips in this article are particularly the very best we've ever had.

by GenFX on Tuesday, December 20th 2011 at 04:03 AM

Thanks a lot for providing individuals with a very wonderful possiblity to discover important secrets from this web site. It's always so enjoyable and as well , stuffed with a great time for me personally and my office colleagues to search your website at the least three times in a week to read the new stuff you have. And lastly, I'm so always fascinated with all the magnificent suggestions you give. Certain 4 tips in this article are particularly the very best we've ever had.

by HGH Advanced on Tuesday, December 20th 2011 at 04:05 AM

Thanks a lot for providing individuals with a very wonderful possiblity to discover important secrets from this web site. It's always so enjoyable and as well , stuffed with a great time for me personally and my office colleagues to search your website at the least three times in a week to read the new stuff you have. And lastly, I'm so always fascinated with all the magnificent suggestions you give. Certain 4 tips in this article are particularly the very best we've ever had.

by Capsiplex on Tuesday, December 20th 2011 at 04:09 AM

Thanks a lot for providing individuals with a very wonderful possiblity to discover important secrets from this web site. It's always so enjoyable and as well , stuffed with a great time for me personally and my office colleagues to search your website at the least three times in a week to read the new stuff you have. And lastly, I'm so always fascinated with all the magnificent suggestions you give. Certain 4 tips in this article are particularly the very best we've ever had.

by Meratol on Tuesday, December 20th 2011 at 04:11 AM

Thanks a lot for providing individuals with a very wonderful possiblity to discover important secrets from this web site. It's always so enjoyable and as well , stuffed with a great time for me personally and my office colleagues to search your website at the least three times in a week to read the new stuff you have. And lastly, I'm so always fascinated with all the magnificent suggestions you give. Certain 4 tips in this article are particularly the very best we've ever had.

by Proactol Plus on Tuesday, December 20th 2011 at 04:25 AM

Thanks a lot for providing individuals with a very wonderful possiblity to discover important secrets from this web site. It's always so enjoyable and as well , stuffed with a great time for me personally and my office colleagues to search your website at the least three times in a week to read the new stuff you have. And lastly, I'm so always fascinated with all the magnificent suggestions you give. Certain 4 tips in this article are particularly the very best we've ever had.

by Nuratrim on Tuesday, December 20th 2011 at 04:27 AM

Thanks a lot for providing individuals with a very wonderful possiblity to discover important secrets from this web site. It's always so enjoyable and as well , stuffed with a great time for me personally and my office colleagues to search your website at the least three times in a week to read the new stuff you have. And lastly, I'm so always fascinated with all the magnificent suggestions you give. Certain 4 tips in this article are particularly the very best we've ever had.

by HGHAdvanced on Tuesday, December 20th 2011 at 02:44 PM

Thanks a lot for providing individuals with a very wonderful possiblity to discover important secrets from this web site. It's always so enjoyable and as well , stuffed with a great time for me personally and my office colleagues to search your website at the least three times in a week to read the new stuff you have. And lastly, I'm so always fascinated with all the magnificent suggestions you give. Certain 4 tips in this article are particularly the very best we've ever had.

by Waclawa on Saturday, January 14th 2012 at 01:56 AM

Once you have recreated the problem and captured these steps, you can save them to a file and send it to your support person, who can then open it up and view


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 >>
Ads

From the creators of Geekpedia, a revolutionary new coupon website!

BargainEZ has coupons codes, printable coupons, bargains and it is the leading source of Passbook coupons for iPhone and iPod touch devices.

Coupons
Discover Geekpedia
Other Resources