2012-07-10 82 views
6

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

+4

http://stackoverflow.com/questions/148742/how-to-detect-if-any-specific-drive-is-a-hard-drive – Musa 2012-07-10 19:22:46

+4

有一种简单的方法可以找到你要找的答案。它被称为搜索。仅在这个网站上就有很多提出的解决方案。 – TheZ 2012-07-10 19:24:52

+0

@Musa - 这个问题不是我要问的。这个问题是关于检测驱动器是CDROM驱动器还是硬盘驱动器。与我所要求的无关 – Icemanind 2012-07-10 19:40:48

回答

12

使用WMI来检测是否磁盘在CD/DVD驱动器:

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

here

DriveType Enumeration可以帮你什么样的光盘:

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

的一种CD/DVD的/蓝光看到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
  • BD-ROM
  • BDR
  • BD-RE
  • MAX

你的代码可能是这样的:

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

1

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

从上面的链接

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(); 
    } 
} 
+4

由于链接到期,因此仅提供链接并不构成一个好的答案。 http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers/8259#8259 – spender 2012-07-10 19:26:00

+0

你是对的,但是当事情已经存在,为什么不我们重复使用它 – 2012-07-10 19:27:24

+0

我已经发布了代码,然后没有必要投下来。 – 2012-07-10 19:29:51

相关问题