?
Solved

cd/dvd in drive

Posted on 2006-07-13
8
Medium Priority
?
550 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

762 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