Advertise with us

Detect when CD drive was closed

So I am making a little project for myself. I need to detect if the CD Drive was closed, and if it was, run a function.

Is this possible?
Last updated:1/29/2014 12:57:26 AM

1 Answers

Pravesh Singh
Pravesh Singh

Hi Samuel,



As far I understand the question is you want to detect there is a disk in side the drive and running or maybe just one inserted. if so this snippet will help you

using System;
using System.Management;
class Application
{
    public static void Main()
    {
        SelectQuery query = new SelectQuery( "select * from win32_logicaldisk where drivetype=5" );
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
        foreach( ManagementObject mo in searcher.Get() )
        {
          // If both properties are null I suppose there's no CD
             if( ( mo["volumename"] != null ) || ( mo["volumeserialnumber"] != null ) )
             {
                 Console.WriteLine( "CD is named: {0}", mo["volumename"] );
                 Console.WriteLine( "CD Serial Number: {0}", mo["volumeserialnumber"] );
             }
             else
             {
                 Console.WriteLine( "No CD in Unit" ); // Here you can make sure there is no disk.
             }
        }
        // Here to stop app from closing
        Console.WriteLine( "\nPress Return to exit." );
        Console.Read();
   }
}

Last updated:1/29/2014 12:57:26 AM

Answer