Check for disk in CD/DVD drive
How do I check to see if there is a CD or DVD in a specified drive in vb.net.
This is how i did it in VB Classic:
VB Code:
Public Function CheckDrive() As Boolean
Dim lngTotalClusters As Long
CheckDrive = True
If (GetDiskFreeSpace(drivePath & ":\", 0&, 0&, 0&, lngTotalClusters) = 0) Then
CheckDrive = False
End If
End Function
...but this doesnt seem to work
Re: Check for disk in CD/DVD drive
Never mind! I just modified the function and it worked...
VB Code:
Public Function CheckDrive() As Boolean
Dim lngTotalClusters As Long
CheckDrive = True
If (GetDiskFreeSpace(drivePath & ":\", byVal 0&, byVal 0&, byVal 0&, lngTotalClusters) = 0) Then
CheckDrive = False
End If
End Function
Re: [RESOLVED] Check for disk in CD/DVD drive
Scratch that.... it doesnt seem to work with those changes either....
Re: [RESOLVED] Check for disk in CD/DVD drive
Code:
Try
'code
Catch ex as exception
Messagebox.Show("Error: Ensure the disk is in the drive!")
End Try
Re: Check for disk in CD/DVD drive
You can use WMI, specifically the Win32_CDRomDrive WMI class. Check for the "MediaLoaded" property, whether it is True or False. If true, then there is media present, and you can get info on the media, like volume name and size. Check my sig for the WMI codebank submission in order for an example on using it, the MSDN documentation on the Win32_CDROMDrive class is found here: http://msdn.microsoft.com/library/de...re_classes.asp (click on it in the list on the left)
**Note - the Win32_CDROMDrive WMI class will also show DVD drives. The mediatype property will still show as "CD-ROM", but the Size will still reflect the actual size of the DVD media
Re: Check for disk in CD/DVD drive
Quote:
Originally Posted by gigemboy
You can use WMI, specifically the Win32_CDRomDrive WMI class. Check for the "MediaLoaded" property, whether it is True or False. If true, then there is media present, and you can get info on the media, like volume name and size. Check my sig for the WMI codebank submission in order for an example on using it, the MSDN documentation on the Win32_CDROMDrive class is found here:
http://msdn.microsoft.com/library/de...re_classes.asp (click on it in the list on the left)
**Note - the Win32_CDROMDrive WMI class will also show DVD drives. The mediatype property will still show as "CD-ROM", but the Size will still reflect the actual size of the DVD media
Thanks. can you give me a simple example of how i would use this in my code?
Re: Check for disk in CD/DVD drive
Quote:
Originally Posted by gigemboy
Check my sig for the WMI codebank submission in order for an example on using it
as stated above...
Re: Check for disk in CD/DVD drive
Sorry for the oversight...
I added the WMI reference to my program and used this bit of code and it doesnt work...
VB Code:
Dim MyDrive As New Win32.CDROMDrive
'gets all instances of the class, loops through
For Each Drive As Win32.CDROMDrive In MyDrive.GetInstances()
MessageBox.Show(Drive.Caption)
MessageBox.Show(Drive.Status)
Next
visual basic 2005 doesnt like "MyDrive.GetInstances()" any idea wat the problem is?
Re: Check for disk in CD/DVD drive
I already have the drive letter and I just want to be able to check if a CD/DVD is in the drive and get the name of the volume if it is there.
I don't want to have to loop through all the drives. Is there a way to check one specific drive?
Re: Check for disk in CD/DVD drive
The looping of the drives doesnt take that long. Here is the code that should do it. Below doesnt require the WMI dll in my codebank submission, but it does require a reference to System.Management...
VB Code:
Dim MyDriveLetter As String = "F:"
Dim Found As Boolean = False
Dim MyobjectQuery As New System.Management.ObjectQuery("select * from Win32_CDROMDrive")
Dim MySearcher As New System.Management.ManagementObjectSearcher(MyobjectQuery)
For Each Mgmt As System.Management.ManagementObject In MySearcher.Get()
If Mgmt("ID").ToString = MyDriveLetter AndAlso Mgmt("MediaLoaded").ToString = "True" Then
Found = True
Exit For
End If
Next
If Found = True Then
MessageBox.Show("Media is loaded on drive " & MyDriveLetter)
Else
MessageBox.Show("Media is NOT loaded on drive " & MyDriveLetter)
End If