A 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.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This 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.
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 24 votes)
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:
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.
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:
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:
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:
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:
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 It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList|
Rate this tutorial
There are no related tutorials.
Related Source Code
There is no related source code.
C# Job Search