2012-07-10 95 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

相關問題