Click here to Skip to main content
Licence CPOL
First Posted 24 Jan 2005
Views 39,702
Downloads 235
Bookmarked 21 times

Open and Close CD drive in C#

By | 24 Jan 2005 | Article
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Jason Iwachow



Canada Canada

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalmore then one CD drive ... PinsussAnonymous4:24 24 Jan '05  
GeneralRe: more then one CD drive ... Pinmembermav.northwind6:27 24 Jan '05  
GeneralIncorrect mciSendStringA declaration Pinmembermav.northwind1:23 24 Jan '05  
GeneralRe: Incorrect mciSendStringA declaration PinsitebuilderUwe Keim5:56 24 Jan '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 24 Jan 2005
Article Copyright 2005 by Jason Iwachow
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid