|
-
Mar 30th, 2006, 07:14 PM
#1
Thread Starter
Junior Member
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
Last edited by hobbes487; Mar 30th, 2006 at 07:38 PM.
-
Mar 30th, 2006, 07:18 PM
#2
Thread Starter
Junior Member
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
-
Mar 30th, 2006, 07:34 PM
#3
Thread Starter
Junior Member
Re: [RESOLVED] Check for disk in CD/DVD drive
Scratch that.... it doesnt seem to work with those changes either....
-
Mar 30th, 2006, 07:42 PM
#4
Fanatic Member
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
-
Mar 30th, 2006, 07:43 PM
#5
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
-
Mar 30th, 2006, 07:56 PM
#6
Thread Starter
Junior Member
Re: Check for disk in CD/DVD drive
 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?
-
Mar 31st, 2006, 12:38 AM
#7
Re: Check for disk in CD/DVD drive
 Originally Posted by gigemboy
Check my sig for the WMI codebank submission in order for an example on using it
as stated above...
-
Mar 31st, 2006, 01:20 AM
#8
Thread Starter
Junior Member
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?
-
Mar 31st, 2006, 02:36 AM
#9
Thread Starter
Junior Member
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?
-
Mar 31st, 2006, 12:36 PM
#10
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
Last edited by gigemboy; Mar 31st, 2006 at 01:04 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|