How to Detect Insertion of a CD ROM using C#

This C# code detects a CD-ROM being inserted into the CD-ROM drive of a PC. In order to interact with the CD-ROM, you need to use Windows Management Instrumentation (WMI). The .NET Framework provides two sets of classes for interaction with WMI. The System.Management Namespace provides access to management information and events about the system. TheSystem.Management.Instrumentation Namespace provides classes to expose management information and events of an application to WMI.

using System;
using System.Management;

namespace CDROMManagement
  class WMIEvent
    static void Main(string[] args)
      WMIEvent we = new WMIEvent();
      ManagementEventWatcher w = null;
      WqlEventQuery q;
      ManagementOperationObserver observer = new

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

        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 );

        // Do something usefull,block thread for testing
      catch( Exception e )
        Console.WriteLine( e.Message );

    // Dump all properties
    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)
          Console.WriteLine("CD has been inserted");
          Console.WriteLine("CD has been ejected");

Note… In order to use the System.Management namespace in .NET 2, you will need to add a reference to the System.Management.dll. This can be done in the IDE by right-clicking on the project in the solution explorer and choosing Add Reference… from the list. The DLL is on the first tab towards the end of the list of items.

15 responses to this post.

  1. naturally like your web site however you have to check the spelling on several of your posts. A number of them are rife with spelling problems and I in finding it very bothersome to inform the reality then again I will definitely come back again.

  2. I use WordPress for all my sites. My current web site, Rap Tweets, uses WordPress for a CMS. And within that web-site is a “blog”a blog within a blog! This is possible as a result of WordPress programmingsomething you forgot to mention too. It’s quite simple to customize WordPress using programming functions.

  3. It’s an honor to possess the free chemistry globe magazine app.

  4. Just to let you know your webpage looks a little bit different on Safari on my laptop with Linux .

  5. Just to let you know your webpage appears a little bit strange on Firefox on my notebook using Linux .

  6. Re: Whoever made the statement that this was an excellent site actually needs to have their brain examined.

  7. If you dont mind, where do you host your weblog? I am shopping for a good web host and your site appears to be fast and up just about all the time

  8. Just to let you know your website appears a little bit strange in Firefox on my pc using Linux .

  9. When I start your Rss feed it seems to be a lot of junk, is the issue on my part?

  10. Oh man! This site is amazing! How can I make it look like this !

  11. I adore that blog layout ! How do you make it!? It is so nice.

  12. I’m a lengthy time watcher and I just thought I’d drop by and say hello there there for your really initial time.

  13. One more thing that I wish for to share here is that, whatever you are using free blogging service however if you don’t update your web site on regularly basis then it’s no more importance.Hmmm, yup no doubt Google is finest in support of blogging but today word press is also nice as a blogging because its Web optimization is good defined already.

  14. If you apply such methods for increasing traffic on your own web site, I am certainly you will get the variation in few days. There is also one other technique to increase traffic in support of your blog that is link exchange, so you as well try it.

  15. the time to study or stop by the subject material or web-sites we’ve linked to beneath the

Comments are closed.

%d bloggers like this: