2012-07-10 15 views
6

C'è un modo semplice per rilevare se un disco è inserito nell'unità DVD? Non mi interessa che tipo di disco (CD, DVD o Blu-Ray)?Rilevare se il disco è nell'unità DVD

+4

http://stackoverflow.com/questions/148742/how-to-detect-if-any-specific-drive-is-a-hard-drive – Musa

+4

C'è un modo semplice per trovare la risposta che cerchi. Si chiama ricerca. Solo su questo sito ci sono una miriade di soluzioni proposte. – TheZ

+0

@Musa - Quella domanda non è quello che sto chiedendo. Questa domanda riguarda il rilevamento se un'unità è un'unità cdrom o un disco rigido. Niente a che vedere con quello che sto chiedendo – Icemanind

risposta

12

utilizzare WMI per rilevare se il disco nel drive CD/DVD:

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

da here.

DriveType Enumeration può aiutare voi che tipo di disco:

  • CDRom: L'unità è un dispositivo di disco ottico, ad esempio un CD o DVD-ROM.
  • Fixed: L'unità è un disco fisso.
  • Network: L'unità è un'unità di rete.
  • NoRootDirectory L'unità non dispone di una directory principale.
  • Ram: L'unità è un disco RAM.
  • Removable: L'unità è un dispositivo di memorizzazione rimovibile, ad esempio un'unità disco floppy o un'unità flash USB.
  • Unknown: Il tipo di unità non è nota.

per tipo di CD/DVD/Blu-Ray vedi 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

il codice può essere simile a questo:

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

da here.

1

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

Dal link qui sopra

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

Poiché i collegamenti scadono, fornire semplicemente un collegamento non costituisce una buona risposta. http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers/8259#8259 – spender

+0

hai ragione, ma quando una cosa è già uscita perché no lo riutilizziamo –

+0

Ho pubblicato il codice ora, quindi non c'è bisogno di votare giù. –

Problemi correlati