Solved

Gettings a List of Current Drives

Posted on 2006-07-10
6
208 Views
Last Modified: 2010-04-23
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.
0
Comment
Question by:Linky
  • 3
  • 2
6 Comments
 
LVL 3

Accepted Solution

by:
concept99 earned 50 total points
ID: 17074687
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
 
LVL 2

Author Comment

by:Linky
ID: 17074713
There is no way to do with with DirectoryInfo or something?
0
 
LVL 41

Expert Comment

by:graye
ID: 17074750
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 41

Expert Comment

by:graye
ID: 17074763
Oops, my example sucks

For each Drive As System.IO.DriveInfo in My.Computer.FileSystem.Drives
   ' do Something
Next
0
 
LVL 2

Author Comment

by:Linky
ID: 17074774
Im using 2002 so that probably won't work.
0
 
LVL 2

Author Comment

by:Linky
ID: 17075192
That does the trick in my program concept99. Thank you.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now