Gettings a List of Current Drives

I am using Visual Basic .NET. Is there a way to get a list of the current disk drives (CD-ROM included) on the specific system? For example: C:, D:, E:.. if they exist? Code would help. Thank you.
LVL 2
LinkyAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
concept99Connect With a Mentor Commented:
Using WMI is probably your best bet in this situation. Here's the code:

Imports System
Imports System.Management
Imports System.Windows.Forms

Namespace WMISample

    Public Class MyWMIQuery

        Public Overloads Shared Function Main() As Integer

            Try
                Dim searcher As New ManagementObjectSearcher( _
                    "root\CIMV2", _
                    "SELECT * FROM Win32_LogicalDisk")

                For Each queryObj As ManagementObject in searcher.Get()

                    Console.WriteLine("-----------------------------------")
                    Console.WriteLine("Win32_LogicalDisk instance")
                    Console.WriteLine("-----------------------------------")
                    Console.WriteLine("Caption: {0}", queryObj("Caption"))
                    Console.WriteLine("Description: {0}", queryObj("Description"))
                    Console.WriteLine("FileSystem: {0}", queryObj("FileSystem"))
                Next
            Catch err As ManagementException
                MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
            End Try
        End Function
    End Class
End Namespace
0
 
LinkyAuthor Commented:
There is no way to do with with DirectoryInfo or something?
0
 
grayeCommented:
The newer version of Visual Basic (version 2005) has the "My" namespace, so if you're using that version, it's as simple as:

For each DriveLetter as string in My.Computer.FileSystem.Drives
    ' do something
Next
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
grayeCommented:
Oops, my example sucks

For each Drive As System.IO.DriveInfo in My.Computer.FileSystem.Drives
   ' do Something
Next
0
 
LinkyAuthor Commented:
Im using 2002 so that probably won't work.
0
 
LinkyAuthor Commented:
That does the trick in my program concept99. Thank you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.