How to Detect CD-ROM is loaded in the CD-ROM drive using C#

This C# code detects if a CD-ROM is loaded in 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;

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 to stop app from closing
    Console.WriteLine( "\nPress Return to exit." );

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.


34 responses to this post.

  1. Thanks Camden, I’ll check out yours shortly

  2. I’m hoping for getting my entry for the Umbrella Prints Trimmings comp done this week 🙂

  3. I’d appreciate to have all of my blocks sewn together for my Mountain Mystery.

  4. I’ve been using WordPress for that past 3-4 ages and i definitely similar to this CMS. Its genuinely amazing. Hats off to matt for providing this excellent blogging software

  5. This really answered my problem, thank you!

  6. When are you going to post again? You really inform a lot of people!

  7. It looks to me that this web site doesnt load up in a Motorola Droid. Are other people having the exact same problem? I enjoy this blog and dont want to have to miss it any time Im gone from my computer.

  8. Have you considered including a few social bookmarking links to these sites. At the very least for myspace.

  9. I was basically curious about if you ever thought of adjusting the page layout of your site? Its well written; I love what you have got to state. But maybe you can create a little more in the way of written content so people could connect to it better. Youve got a great deal of wording for only having one or two pictures. Maybe you can space it out better?

  10. How did you make your blog look this cool. Email me if you want and share your wisdom. Id be thankful!

  11. I believe that one of your current advertisements caused my web browser to resize, you may well want to put that on your blacklist.

  12. This really answered my problem, thank you!

  13. The design for the site is a tad off in Epiphany. Nevertheless I like your web site. I may need to install a normal browser just to enjoy it.

  14. Jesus Christ there is a great deal of spammy feedback on this webpage. Have you ever believed about attempting to get rid of them or putting in a tool?

  15. How come you do not have your web site viewable in wap format? cant view anything in my netbook.

  16. This really answered my problem, thank you!

  17. Im getting a small problem. I cant get my reader to pickup your rss feed, Im using msn reader by the way.

  18. Great Stuff, do you have a myspace account?

  19. Is it fine to place part of this on my personal weblog if perhaps I post a reference point to this web site?

  20. Just discovered this site through Google, what a way to brighten up my week!

  21. Make money on new film releases! Meet the James Bond of Social Networking. Would you like to be the first person out of the gate to make money on feature film releases?

  22. Nice post my friend , just to let you know , i give you a google+1 also i share your link on my facebook timeline and my twitter account , keep writen .

  23. I’ll gear this review to 2 types of people: current Zune owners who are considering an upgrade, and people trying to decide between a Zune and an iPod. (There are other players worth considering out there, like the Sony Walkman X, but I hope this gives you enough info to make an informed decision of the Zune vs players other than the iPod line as well.)

  24. This can be the worst post of all, I’ve read

  25. Hi there, just became aware of your blog through Google, and found that it’s truly informative. I am going to watch out for brussels. I will appreciate if you continue this in future. Lots of people will be benefited from your writing. Cheers!

  26. I think your site is pretty helpful, i found it from a good friend of mine from Thailand

  27. Studying by way of your nice content material, will support me to do so from time to time.

  28. Wow! This could be one particular of the most useful blogs We have ever arrive across on this subject. Basically Excellent. I am also an expert in this topic therefore I can understand your hard work.

  29. r u sure that is true?

  30. hello there and thanks to your info – I’ve definitely picked up something new from right here. I did however experience some technical issues using this site, as I experienced to reload the website many instances previous to I may get it to load correctly. I were thinking about if your hosting is OK? Now not that I am complaining, but sluggish loading instances instances will very frequently impact your placement in google and could injury your high quality ranking if advertising and ***********|advertising|advertising|advertising and *********** with Adwords. Well I’m including this RSS to my e-mail and could glance out for a lot extra of your respective exciting content. Ensure that you replace this once more soon..

  31. Deference to article author , some good entropy.

  32. Undeniably believe that which you stated. Your favorite reason appeared to be on the net the simplest thing to be aware of. I say to you, I certainly get irked while people consider worries that they just do not know about. You managed to hit the nail upon the top as well as defined out the whole thing without having side effect , people can take a signal. Will probably be back to get more. Thanks

  33. good site thanks!

  34. As soon as I observed this site I went on reddit to share some of the love with them.

Comments are closed.

%d bloggers like this: