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
Solved

cd/dvd in drive

Posted on 2006-07-13
8
546 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VS.net 2010 11 48
Sending receiving text messages in vb.net 15 48
Calculate number of nights between two dates 5 53
location of a form 2 25
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

808 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