Click here to Skip to main content
15,883,812 members
Articles / Programming Languages / C#

Open and Close CD drive in C#

Rate me:
Please Sign up or sign in to vote.
2.63/5 (10 votes)
24 Jan 2005CPOL 77.4K   2.4K   21  
An article on how to open and close the CD drive from your C# program.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

namespace OpenCloseCD
{

	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button openb;
		private System.Windows.Forms.Button closeb;

		private System.ComponentModel.Container components = null;

		public Form1()
		{InitializeComponent();}

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.openb = new System.Windows.Forms.Button();
			this.closeb = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// openb
			// 
			this.openb.Location = new System.Drawing.Point(0, 8);
			this.openb.Name = "openb";
			this.openb.TabIndex = 0;
			this.openb.Text = "open";
			this.openb.Click += new System.EventHandler(this.openb_Click);
			// 
			// closeb
			// 
			this.closeb.Location = new System.Drawing.Point(96, 8);
			this.closeb.Name = "closeb";
			this.closeb.TabIndex = 1;
			this.closeb.Text = "close";
			this.closeb.Click += new System.EventHandler(this.closeb_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(176, 45);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.closeb,
																		  this.openb});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.Name = "Form1";
			this.Text = "CD CONTROL";
			this.ResumeLayout(false);

		}
		#endregion

		[STAThread]
		static void Main() 
		{Application.Run(new Form1());}

		/// <summary>
		/// Importing this god forbiden dll
		/// </summary>
		[DllImport("winmm.dll", EntryPoint="mciSendStringA")]
		public static extern void mciSendStringA(string lpstrCommand, string lpstrReturnString, long uReturnLength, long hwndCallback);
		
		//Why did i put this here?
		string rt = "";

		private void openb_Click(object sender, System.EventArgs e)
		{mciSendStringA("set CDAudio door open",rt,127,0);}//i remember, hehe.

		private void closeb_Click(object sender, System.EventArgs e)
		{mciSendStringA("set CDAudio door closed",rt,127,0);}// YODA FOR LIFE PEACE!
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions