---
title: "Detect when CD drive was closed"  
description: "Detect when CD drive was closed"  
author: "Anonymous User"  
published: 2014-01-29  
updated: 2014-01-29  
canonical: https://www.mindstick.com/forum/1906/detect-when-cd-drive-was-closed  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# Detect when CD drive was closed

So I am making a little [project](https://www.mindstick.com/articles/105927/how-to-excel-at-managing-multiple-projects) for myself. I need to [detect](https://www.mindstick.com/news/2397/smartwatches-with-skin-like-electronics-and-ai-could-aid-in-the-early-detection-of-health-issues) if the CD Drive was closed, and if it was, run a function.

Is this possible?

## Replies

### Reply by 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();   }}
```


---

Original Source: https://www.mindstick.com/forum/1906/detect-when-cd-drive-was-closed

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
