IT Community - Software Programming, Web Development and Technical Support

How to detect a CD Drive in C#?

This is a discussion on How to detect a CD Drive in C#? within the C# Programming forums, part of the Software Development category; Hi, I want to detect a CD drive using C#. I have to get a message when the CD is ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 10-27-2007, 04:55 AM
oxygen oxygen is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 633
oxygen is on a distinguished road
Default How to detect a CD Drive in C#?

Hi,

I want to detect a CD drive using C#. I have to get a message when the CD is inserted in a CD drive and Also get a message when a CD is removed. I also want to open a cd in my application. Can any anyone give me an idea to do this?
__________________
The OXYGEN
Delivers edgy, intelligent Technology to all...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-27-2007, 05:00 AM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 182
Balasubramanian.S is on a distinguished road
Default Re: How to detect a CD Drive in C#?

Hi,

You can do this by using the following code. This code helps you to to detect a cd drive.

using System.Management;

private void Form1_Load(object sender, EventArgs e)
{
Form1 we = new Form1();
ManagementEventWatcher w = null;
WqlEventQuery q;
ManagementOperationObserver observer = new
ManagementOperationObserver();

// Bind to local machine
ConnectionOptions opt = new ConnectionOptions();
opt.EnablePrivileges = true; //sets required privilege
ManagementScope scope = new ManagementScope("root\\CIMV2", opt);

try
{
q = new WqlEventQuery();
q.EventClassName = "__InstanceModificationEvent";
q.WithinInterval = new TimeSpan(0, 0, 1);

// DriveType - 5: CDROM
q.Condition = @"TargetInstance ISA 'Win32_LogicalDisk' and
TargetInstance.DriveType = 5";
w = new ManagementEventWatcher(scope, q);

// register async. event handler
w.EventArrived += new EventArrivedEventHandler(we.CDREventArrived);
w.Start();

// Do something usefull,block thread for testing
Console.ReadLine();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
w.Stop();
}
}
public void CDREventArrived(object sender, EventArrivedEventArgs e)
{
// Get the Event object and display it
PropertyData pd = e.NewEvent.Properties["TargetInstance"];

if (pd != null)
{
ManagementBaseObject mbo = pd.Value as ManagementBaseObject;

// if CD removed VolumeName == null
if (mbo.Properties["VolumeName"].Value != null)
{
MessageBox.Show("CD has been inserted");
}
else
{
MessageBox.Show("CD has been ejected");
}
}
}

Thanks..

S.Balasubramanian..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to detect UserInner browser? S.Vinothkumar HTML, CSS and Javascript Coding Techniques 4 10-07-2007 10:31 PM
How would you detect and minimize memory leaks in Java? oxygen Java Programming 1 07-26-2007 05:01 AM
How would you detect and minimize memory leaks in Java? H2o Java Programming 1 07-24-2007 03:45 AM
How to detect collisions between 3D objects in Java ME (J2ME) M3G ? itbarota J2ME 1 07-19-2007 01:03 AM
How to detect collisions between 3D objects in Java ME (J2ME) M3G ? itbarota J2ME 0 07-17-2007 11:53 PM


All times are GMT -7. The time now is 10:50 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0