Results 1 to 10 of 10

Thread: Check for disk in CD/DVD drive

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2006
    Posts
    18

    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:
    1. Public Function CheckDrive() As Boolean
    2.         Dim lngTotalClusters As Long
    3.         CheckDrive = True
    4.         If (GetDiskFreeSpace(drivePath & ":\", 0&, 0&, 0&, lngTotalClusters) = 0) Then
    5.             CheckDrive = False
    6.         End If
    7.     End Function

    ...but this doesnt seem to work
    Last edited by hobbes487; Mar 30th, 2006 at 07:38 PM.

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Mar 2006
    Posts
    18

    Re: Check for disk in CD/DVD drive

    Never mind! I just modified the function and it worked...

    VB Code:
    1. Public Function CheckDrive() As Boolean
    2.         Dim lngTotalClusters As Long
    3.         CheckDrive = True
    4.         If (GetDiskFreeSpace(drivePath & ":\", byVal 0&, byVal 0&, byVal 0&, lngTotalClusters) = 0) Then
    5.             CheckDrive = False
    6.         End If
    7.     End Function

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2006
    Posts
    18

    Re: [RESOLVED] Check for disk in CD/DVD drive

    Scratch that.... it doesnt seem to work with those changes either....

  4. #4
    Fanatic Member Ruku's Avatar
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    655

    Exclamation 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

    Using VB.NET 2005/.NET 2.0, NetBeans IDE 5, Fujitsu Cobol85,
    Website: http://DreamForgery.com

  5. #5
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Mar 2006
    Posts
    18

    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?

  7. #7
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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...

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Mar 2006
    Posts
    18

    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:
    1. Dim MyDrive As New Win32.CDROMDrive
    2.         'gets all instances of the class, loops through
    3.         For Each Drive As Win32.CDROMDrive In MyDrive.GetInstances()
    4.             MessageBox.Show(Drive.Caption)
    5.             MessageBox.Show(Drive.Status)
    6.         Next

    visual basic 2005 doesnt like "MyDrive.GetInstances()" any idea wat the problem is?

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Mar 2006
    Posts
    18

    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?

  10. #10
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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:
    1. Dim MyDriveLetter As String = "F:"
    2.         Dim Found As Boolean = False
    3.         Dim MyobjectQuery As New System.Management.ObjectQuery("select * from Win32_CDROMDrive")
    4.         Dim MySearcher As New System.Management.ManagementObjectSearcher(MyobjectQuery)
    5.         For Each Mgmt As System.Management.ManagementObject In MySearcher.Get()
    6.             If Mgmt("ID").ToString = MyDriveLetter AndAlso Mgmt("MediaLoaded").ToString = "True" Then
    7.                 Found = True
    8.                 Exit For
    9.             End If
    10.         Next
    11.         If Found = True Then
    12.             MessageBox.Show("Media is loaded on drive " & MyDriveLetter)
    13.         Else
    14.             MessageBox.Show("Media is NOT loaded on drive " & MyDriveLetter)
    15.         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
  •  



Click Here to Expand Forum to Full Width