Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

cd/dvd in drive

Posted on 2006-07-13
8
Medium Priority
?
552 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

604 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