Click here to Skip to main content
15,914,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have looked around and can't find a simple solution to what I want to do.

I want to open a CD-Rom from my C# app. It should check if the media is in fact a cd- rom and then open it. Is there a quick solution to this or am I missing something?
Posted
Updated 1-Aug-11 6:51am
v2

Using the code from Open & close CD tray in C#
Create the following class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

//Namespace references
using System.Runtime.InteropServices;

namespace dummyProj1
{
    class DVD
    {
        //Win32 API Call
        [DllImport("winmm.dll", EntryPoint = "mciSendStringA", CharSet = CharSet.Ansi)]
        protected static extern int mciSendString(string lpstrCommand, StringBuilder lpstrReturnString, int uReturnLength, IntPtr hwndCallback);

        public bool ProcessCDTray(bool open)
        {
            int ret = 0;
            //do a switch of the value passed
            switch (open)
            {
                case true:  //true = open the cd
                    ret = mciSendString("set cdaudio door open", null, 0, IntPtr.Zero);
                    return true;
                    //break;
                case false: //false = close the tray
                    ret = mciSendString("set cdaudio door closed", null, 0, IntPtr.Zero);
                    return true;
                    //break;
                default:
                    ret = mciSendString("set cdaudio door open", null, 0, IntPtr.Zero);
                    return true;
                    //break;
            }
        }
    }
}


To Open the DVD Tray, use the following bit of code:

DVD thedvd = new DVD();
//Open CD Tray
thedvd.ProcessCDTray(true);

//To Close the CD Tray
thedvd.ProcessCDTray(false);
 
Share this answer
 
v3
Try these:

http://en.csharp-online.net/Open/close_CD_drive_tray

http://www.vcskicks.com/disk-drive.php

http://www.dreamincode.net/code/snippet1692.htm
 
Share this answer
 
 
Share this answer
 
Comments
evilheartx 2-Aug-11 2:05am    
I Want To Work With C# Not With C++

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900