Solved

cd/dvd in drive

Posted on 2006-07-13
8
543 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
Comment Utility
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
Comment Utility
JRossi,

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

Expert Comment

by:JRossi1
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now