Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Open and Close CD drive in C#

0.00/5 (No votes)
24 Jan 2005 1  
An article on how to open and close the CD drive from your C# program.

Sample Image - Not really nessesary

Introduction

I was looking for code in C# to open/close the CD drive so I could make a funny program that when run would open the CD drive every five minutes and play with it "Another cup holder for your tea governor?". Yea, anyways, to my surprise, I couldn't find it.

Background

To continue my story, I was searching the net and I found a simple code in VB. Well, I don't know VB, but I do know some other languages, and to my surprise, I was actually able to translate it.

Using the Code

Well, first you have to import a DLL and a variable for the return status.

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

string rt = "";

Now, all you have to do is call it like this to open/ close the CD drive:

mciSendStringA("set CDAudio door open",rt,127,0);

mciSendStringA("set CDAudio door closed",rt,127,0);

Points of Interest

It's pretty short. I'm pretty happy with the results of that British joke!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here