Home > DeveloperSection > Forums > Detect when CD drive was closed
Simons Hood
Simons Hood

Total Post:70

Posted on    January-29-2014 12:07 AM

 C# C# 

 1 Reply(s)
 662  View(s)
Rate this:

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?

Pravesh Singh

Total Post:411

Posted on    January-29-2014 12:57 AM

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




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




Don't want to miss updates? Please click the below button!

Follow MindStick