Solved

How do you just return the hard drives on a pc

Posted on 2004-08-18
4
266 Views
Last Modified: 2010-04-23
Hello I’m trying to get a list of all the local hard drives on a pc for an application.
I’m currently using       System.IO.Directory.GetLogticalDrives

This works really well but returns things like the floppy drive, cd drive, network drives and flash sticks. Is there a way to just get the local hard drives returned.

Thanks
0
Comment
Question by:FRIBBLE1
4 Comments
 
LVL 28

Accepted Solution

by:
iboutchkine earned 200 total points
ID: 11829610
Sub GetLocalDrives()
      ' Use the System.Management classes to invoke WMI
      ' to get a list of local drives
      '
      ListView1.Items.Clear()
      
      Dim MyQuery As Management.ManagementObjectSearcher = New Management.ManagementObjectSearcher("SELECT * FROM Win32_LogicalDisk")
      Dim MyQueryResults As Management.ManagementObjectCollection = MyQuery.Get()
      Dim tmpItem As New ListViewItem()
      
      Dim MyObject As Management.ManagementObject

      For Each MyObject In MyQueryResults
          tmpItem = ListView1.Items.Add(MyObject.ToString.Trim)
          tmpItem.SubItems.Add(MyObject.Item("DriveType").ToString())
      Next

End Sub
0
 
LVL 17

Assisted Solution

by:AerosSaga
AerosSaga earned 200 total points
ID: 11830403
Imports System.IO

Public Function ListAllDrives() As String()
        Dim arDrives() As String
        arDrives = Directory.GetLogicalDrives()
        Return arDrives
End Function

'SAMPLE USAGE
        Dim sDrive As String, sDrives() As String
        sDrives = ListAllDrives()
        For Each sDrive In sDrives
            Debug.WriteLine(sDrive)
        Next

And for Drive volumes:

Public Declare Function GetVolumeSerialNumber Lib "kernel32" Alias "GetVolumeInformationA" (ByVal lpRootPathName As String, ByVal lpVolumeNameBuffer As Long, ByVal nVolumeNameSize As Long, lpVolumeSerialNumber As Long, ByVal lpMaximumComponentLength As Long, ByVal lpFileSystemFlags As Long, ByVal lpFileSystemNameBuffer As Long, ByVal nFileSystemNameSize As Long) As Long

Public Function VolumeSerial(DriveLetter) As Long
    Dim Serial As Long
    Call GetVolumeSerialNumber(UCase(DriveLetter) & ":\", 0&, 0&, Serial, 0&, 0&, 0&, 0&)
    VolumeSerial = Serial
End Function

 
Example Usage

MsgBox VolumeSerial("C")

Regards,

Aeros
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Gridview selected row 9 49
Adding Existing Item to vb.net Project causes Errors 4 45
Help with opening combobox using VB.NET. 3 26
FInd Image Control Gridview 3 22
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

828 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