C# Snippets


  1. How do I detect whether CD-ROM was inserted into the CD-ROM drive?

  1. Here's how you can do it using System.Management classes (and WMI).
using System;
using System.Management;

namespace snip002
{
  class WMIEvent
  {
    static void Main(string[] args)
    {
      WMIEvent we = new WMIEvent();
      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 e )
      {
        Console.WriteLine( e.Message );
      }
      finally
      {
        w.Stop();
      }
    }

    // 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");
        }
        else
        {
          Console.WriteLine("CD has been ejected");
        }
      }
    }
  }
}

I have created a little application to demo in C# Express.

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 C# Express 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.


References

For more information on the System.Management Namespace visit MSDN at microsoft here.

For more information on the ManagementEventWatcher Class visit MSDN at microsoft here.

For more information on the WqlEventQuery Class visit MSDN at microsoft here.

For more information on the ManagementOperationObserver Class visit MSDN at microsoft here.

For more information on the ManagementScope Class visit MSDN at microsoft here.

For more information on the ManagementBaseObject Class visit MSDN at microsoft here.

For more information on the Win32_LogicalDisk class visit MSDN at microsoft here.

Back to Snippets.