Solved

cd/dvd in drive

Posted on 2006-07-13
8
545 Views
Last Modified: 2009-02-05
Guys,

How can I determine whether a CD or DVD is in the drive?  I do NOT need to autoplay it, I just need to browse to the CD so I can display the files and folders. Currently, my code blowes up with a "device not ready" message when I try to navigate to the drive (in code) and there is no media in there.

Thanks.
0
Comment
Question by:imkiosks
  • 3
  • 2
8 Comments
 
LVL 6

Expert Comment

by:JRossi1
ID: 17102488
Consider this:  The 'IsReady' method will tell you there is valid media in the drive.

Function ShowSysDrives() As String
Dim fso As FileSystemObject
Dim drvDrives As Drives
Dim d
Dim strDrvInfo As String
Set fso = New FileSystemObject
Set drvDrives = fso.Drives
For Each d In drvDrives
   strDrvInfo = strDrvInfo & "Drive: " & _
      d.DriveLetter & vbCrLf & "  Is Ready: " & _
      d.IsReady
   If (d.IsReady) Then
      strDrvInfo = strDrvInfo & vbCrLf & _
         "Volume: " & d.VolumeName & vbCrLf & _
         "Share Name: " & d.ShareName & vbCrLf _
         & "Avl.Space: " & d.AvailableSpace & _
         vbCrLf & "Drive Type: " & d.DriveType
   End If
   strDrvInfo = strDrvInfo & vbCrLf & vbCrLf
Next
ShowSysDrives = strDrvInfo
End Function

0
 

Author Comment

by:imkiosks
ID: 17102549
JRossi,

Your code is VB6. I need VB .net code.
0
 
LVL 6

Expert Comment

by:JRossi1
ID: 17102694
Oops.  My bad.   The following VB.NET code detects the insertion/ejection of a cd into the drive.  This may not be exactly what you are looking for, but should help.


Imports System
Imports System.Management



Class WMIEvent

    Shared Sub Main(ByVal args() As String)
        Dim we As New WMIEvent
        Dim w As ManagementEventWatcher = Nothing
        Dim q As WqlEventQuery
        Dim observer As New ManagementOperationObserver

        ' Bind to local machine
        Dim opt As New ConnectionOptions
        opt.EnablePrivileges = True 'sets required privilege
        Dim scope As New ManagementScope("root\CIMV2", opt)

        Try
            q = New WqlEventQuery
            q.EventClassName = "__InstanceModificationEvent"
            q.WithinInterval = New TimeSpan(0, 0, 1)

            ' DriveType - 5: CDROM
            q.Condition = "TargetInstance ISA 'Win32_LogicalDisk'" & _
                " and TargetInstance.DriveType = 5"
            w = New ManagementEventWatcher(scope, q)

            ' register async. event handler
            AddHandler w.EventArrived, AddressOf we.CDREventArrived
            w.Start()

            ' Do something usefull,block thread for testing
            Console.ReadLine()
        Catch e As Exception
            Console.WriteLine(e.Message)
        Finally
            w.Stop()
        End Try

    End Sub 'Main


    ' Dump all properties
    Public Sub CDREventArrived(ByVal sender As Object, ByVal e As EventArrivedEventArgs)
        ' Get the Event object and display it
        Dim pd As PropertyData = e.NewEvent.Properties("TargetInstance")

        If Not (pd Is Nothing) Then
            Dim mbo As ManagementBaseObject = pd.Value '
            'ToDo: Error processing original source shown below
            '      {
            '        ManagementBaseObject mbo = pd.Value as ManagementBaseObject;
            '---------------------------------------------^--- Syntax error: ';' expected

            ' if CD removed VolumeName == null
            If Not (mbo.Properties("VolumeName").Value Is Nothing) Then
                Console.WriteLine("CD has been inserted")
            Else
                Console.WriteLine("CD has been ejected")
            End If
        End If

    End Sub 'CDREventArrived
End Class 'WMIEvent

0
 

Author Comment

by:imkiosks
ID: 17102864
ok, this code seems to be relying on an event. I'm not using an event. I have to be able to do this at will. The event parameter 'e' seems to be holding all the magic. Since I won't be using an event, how can I get this/similar info?
0
 
LVL 6

Accepted Solution

by:
JRossi1 earned 125 total points
ID: 17103096
I haven't had time to fully vet this, but I think you can use this code to get information about the drive.  

        Dim objMgmt As New ManagementObject
        Dim objOS As ManagementObjectSearcher
        Dim strRetVal as string

        objOS = New ManagementObjectSearcher("select * from Win32_LogicalDisk WHERE DriveType = 5")
        For Each objMgmt In objOS.Get
            strRetVal= objMgmt("PROPERTYNAME").ToString() <------ Don't know the property name we need
        Next


I have to leave, but I will look further into this in the morning.

Hope this helps...

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question