Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting if disc is in DVD drive

Tags:

c#

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)?

like image 638
Icemanind Avatar asked Jul 10 '12 19:07

Icemanind


2 Answers

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.

like image 176
Ria Avatar answered Sep 21 '22 06:09

Ria


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();
    }
}
like image 27
Waqar Janjua Avatar answered Sep 20 '22 06:09

Waqar Janjua