Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to interact with CD-Rom Programmatically

Author: DevASP
Download Source Code : 452_OpenCloseCDROM.zip

In this article I will try to explain you how you can interact with CD-Rom programmatically using window's API in C#.

Create a new application using c#.net. Drag a label and two buttons on the from.

Include the following name namespaces in the project to use the API's:

using System.Runtime.InteropServices;

using System.Text;

Now create a class which have a methods to invoke windows DLL:

public class CD_ROM_API

{

[DllImport("winmm.dll", EntryPoint="mciSendStringA")]

public static extern int mciSendString (string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback);

[DllImport("kernel32.dll", EntryPoint="GetVolumeInformationA")]

public static extern int GetVolumeInformation (string lpRootPathName,StringBuilder lpVolumeNameBuffer, int nVolumeNameSize, int lpVolumeSerialNumber, int lpMaximumComponentLength, int lpFileSystemFlags, string lpFileSystemNameBuffer, int nFileSystemNameSize);

[DllImport("kernel32.dll", EntryPoint="GetDriveTypeA")]

public static extern int GetDriveType (string nDrive);

}

The following api in the class is used to send the string message to media control interface device like the CD-Rom

[DllImport("winmm.dll", EntryPoint="mciSendStringA")]

public static extern int mciSendString (string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback);

 

The following api used to get information about a drive e.g: Its name, seial number if this function return zero means that one of the information could not be retrived so if it is a CD ROM drive and we can't obtain its name ---> CD ROM is empty

 

[DllImport("kernel32.dll", EntryPoint="GetVolumeInformationA")]

public static extern int GetVolumeInformation (string lpRootPathName,StringBuilder lpVolumeNameBuffer, int nVolumeNameSize, int lpVolumeSerialNumber, int lpMaximumComponentLength, int lpFileSystemFlags, string lpFileSystemNameBuffer, int nFileSystemNameSize);

 

The following API get the drive type (0:unknown,1:invalid path, 2:removable(floppy,removabledisk),3:fixed(hard disk),

4:remote(network drive),5:CDROM,6: RAM disk)

 

[DllImport("kernel32.dll", EntryPoint="GetDriveTypeA")]

public static extern int GetDriveType (string nDrive);

 

Now to get the information about the cd-rom write the following code on the form load event

 

private void Form1_Load(object sender, System.EventArgs e)

{

string s ="";

StringBuilder volumeName = new StringBuilder(256);

int srNum = new int();

int comLen = new int();

string sysName = "";

int sysFlags = new int();

int result;

string [] logDrives = System.IO.Directory.GetLogicalDrives();

for(int i=0;i

{

if(CD_ROM_API.GetDriveType(logDrives[i])==5)

{

s+="Your CD ROM is on drive : "+logDrives[i].ToString()+"\n";

result = CD_ROM_API.GetVolumeInformation(logDrives[i].ToString(),volumeName,256,srNum,comLen,sysFlags,sysName,256);

if(result==0)

s+="there is NO CD in ur CD ROM\n";

else

{

s+="There is a CD inside ur CD ROM and its name is " + volumeName+"\n";

}

s+="\n";

}

}

label1.Text=s;

}

 

To open the CD-Rom write the following Code on Open Button click event

 

private void button1_Click(object sender, System.EventArgs e)

{

CD_ROM_API.mciSendString("set CDAudio door open",null, 127, 0);

}

 

To close the CD-Rom write the following Code on close Button click event

 

private void button2_Click(object sender, System.EventArgs e)

{

CD_ROM_API.mciSendString("set CDAudio door closed",null, 127, 0);

}

Article Comments
Didn't know things could be made to look so simple!

Great article. Thanks a lot! Have had frustrating hours, trying to create a burner, but not being able to retrieve ODD info properly, till I finally came across this article.

Once again, thanks a lot!

Posted on 1/9/2010 10:52:38 AM by 3rdEye

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Creating a MDI Tabbed Forms in C#

Disclaimer - Privacy
© 2002-2017 DevASP.net