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 ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| 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... |
| Sponsored Links |
| |||
| 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.. |
![]() |
| Thread Tools | |
| Display Modes | |
| |
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 |