Accessing the CD-ROM
By Mina Fawzi
In this article i will explain how to get informations about the CD-ROM, how to know which drive is the CD-ROM and whether there is a CD inside or not , and also how to open and close the CD-ROM, this is achived by using the win32 api functions which are all declared in a class named api in the following code.
The code is easy and i provide a lettle comment that may help .
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.Text;
namespace CD_ROM
{
///
/// Summary description for Form1.
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support – do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 16);
this.label1.Name = “label1″;
this.label1.Size = new System.Drawing.Size(256, 48);
this.label1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 80);
this.button1.Name = “button1″;
this.button1.Size = new System.Drawing.Size(112, 23);
this.button1.TabIndex = 1;
this.button1.Text = “Open CDROM”;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(144, 80);
this.button2.Name = “button2″;
this.button2.Size = new System.Drawing.Size(112, 23);
this.button2.TabIndex = 2;
this.button2.Text = “Close CDROM”;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(288, 126);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.Name = “Form1″;
this.Text = “CD-ROM”;
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
// intialize tha parameters to the GetVolumeInformation function
string s =”";
StringBuilder volumeName = new StringBuilder(256);
int srNum = new int();
int comLen = new int();
string sysName = “”;
int sysFlags = new int();
int result;
// Directory.GetLogicalDrives return an array of strings that contain
logical drives (c:\,d:\….)
string [] logDrives = System.IO.Directory.GetLogicalDrives();
// for every drive check whether it is a CD ROM or not
for(int i=0;i 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);
//this 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);
}
}
}












No comments yet... Be the first to leave a reply!