6

是否有一种简单的方法可以检测 DVD 驱动器中是否插入了光盘?我不在乎哪种光盘(CD、DVD 或蓝光)?

4

2 回答 2

14

使用 WMI 检测 CD/DVD 驱动器中的磁盘:

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

这里

DriveType Enumeration可以帮助您使用哪种光盘:

  • CDRom:驱动器是光盘设备,例如 CD 或 DVD-ROM。
  • Fixed: 驱动器是固定磁盘。
  • Network:驱动器是网络驱动器。
  • NoRootDirectory 该驱动器没有根目录。
  • Ram: 驱动器是 RAM 盘。
  • Removable:驱动器是可移动存储设备,例如软盘驱动器或 USB 闪存驱动器。
  • Unknown: 驱动器类型未知。

有关 CD/DVD/蓝光的种类,请参见IMAPI_MEDIA_PHYSICAL_TYPE 枚举

  • 未知
  • 光盘
  • CDR
  • CDRW
  • 光驱
  • 影碟机
  • DVDPLUS
  • DVDPLUSRW
  • DVDPLUSR_DUALLAYER
  • DVDDASHR
  • DVDDASHRW
  • DVDDASHR_DUALLAYER
  • 磁盘
  • DVDPLUSRW_DUALLAYER
  • 高清DVD光盘
  • 高清DVDR
  • 硬盘驱动器
  • 只读存储器
  • BDR
  • BDRE
  • 最大限度

你的代码可能是这样的:

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;
}

这里

于 2012-07-10T19:23:16.623 回答
1

如何检测 CD-ROM 是否加载到 CD-ROM 驱动器中

从上面的链接

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();
    }
}
于 2012-07-10T19:22:11.967 回答