7

Is there an easy way to detect if a disc is inserted in the DVD drive? I don't care what kind of disc (CD, DVD or Blu-Ray)?

Icemanind
  • 47,519
  • 50
  • 171
  • 296
  • 4
    http://stackoverflow.com/questions/148742/how-to-detect-if-any-specific-drive-is-a-hard-drive – Musa Jul 10 '12 at 19:22
  • 4
    There is an easy way to do find the answer you seek. It is called searching. On this very site alone there are a plethora of proposed solutions. – TheZ Jul 10 '12 at 19:24
  • @Musa - That question isn't what I'm asking. That question is about detecting if a drive is a cdrom drive or a hard drive. Nothing to do with what I'm asking – Icemanind Jul 10 '12 at 19:40

2 Answers2

15

Use WMI to detect if disk in CD/DVD drive:

foreach (var drive in DriveInfo.GetDrives()
                               .Where(d => d.DriveType == DriveType.CDRom))  
MessageBox.Show(drive.Name + " " + drive.IsReady.ToString()); 

from here.

DriveType Enumeration can help you what kind of disc:

  • CDRom : The drive is an optical disc device, such as a CD or DVD-ROM.
  • Fixed : The drive is a fixed disk.
  • Network : The drive is a network drive.
  • NoRootDirectory The drive does not have a root directory.
  • Ram : The drive is a RAM disk.
  • Removable : The drive is a removable storage device, such as a floppy disk drive or a USB flash drive.
  • Unknown : The type of drive is unknown.

for kind of CD/DVD/Blue-Ray see IMAPI_MEDIA_PHYSICAL_TYPE enumeration:

  • UNKNOWN
  • CDROM
  • CDR
  • CDRW
  • DVDROM
  • DVDRAM
  • DVDPLUSR
  • DVDPLUSRW
  • DVDPLUSR_DUALLAYER
  • DVDDASHR
  • DVDDASHRW
  • DVDDASHR_DUALLAYER
  • DISK
  • DVDPLUSRW_DUALLAYER
  • HDDVDROM
  • HDDVDR
  • HDDVDRAM
  • BDROM
  • BDR
  • BDRE
  • MAX

your code may be like this:

public bool IsDiscAvailable(int driveNumber)
{
    MsftDiscMaster2Class discMaster = new MsftDiscMaster2Class();
    string id = discMaster[driveNumber];

    MsftDiscRecorder2Class recorder = new MsftDiscRecorder2Class();
    recorder.InitializeDiscRecorder(id);

    MsftDiscFormat2DataClass dataWriter = new MsftDiscFormat2DataClass();
    if (dataWriter.IsRecorderSupported(recorder))
    {
        dataWriter.Recorder = recorder;
    }
    else
    {
        Console.WriteLine("Recorder not supported");
        return false;
    }
    if (dataWriter.IsCurrentMediaSupported(recorder))
    {
        var media = dataWriter.CurrentPhysicalMediaType;
        if (media == IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_UNKNOWN)
        {
            Console.WriteLine("Unknown media or no disc");
        }
        else
        {
            Console.WriteLine("Found disc type {0}", media);
            return true;
        }
    }
    else
    {
        Console.WriteLine("Disc absent or invalid.");
    }
    return false;
}

from here.

Ria
  • 10,237
  • 3
  • 33
  • 60
1

How to Detect CD-ROM is loaded in the CD-ROM drive

From above link

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 to stop app from closing
        Console.WriteLine("\nPress Return to exit.");
        Console.Read();
    }
}
MoonKnight
  • 23,214
  • 40
  • 145
  • 277
Waqar Janjua
  • 6,113
  • 2
  • 26
  • 36