Click here to Skip to main content
13,446,015 members (38,132 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi there I am unsure how to go about enumerating or walking through all the hard drives on my computer.

I have 2 internal hard drives and a few external I am able to see all the hard drives in my combobox but I would like to change my approach to how my program works.

I am looking for some advice as to how to detect all connected hard drives and then walk though them one after the other until all hard drives have been searched from start to finish.

Is it possible to return all the hard drives to a string or strings? and then if there is more than one to goto the next?....

thank you in advance
Posted 29-Dec-12 20:38pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The disks connected to a system have multiple aspects from OS point of view. First of all there is the physical device, but there are also partitions, file systems and so on.
Here is a really exhaustive list of disk related tasks you can manage with WMI:[^]. The samples are in vbs, but they can be easily transcribed to Here you have some good examples about querying wmi in[^].
Dale 2012 30-Dec-12 3:15am
ok thank you for your response how would this work, lets say i have no idea what the drive letter is or how many are connected?.... can I still use this method to find all hard drives \ removable drives and search through them one by one until the last one has finished?...
Zoltán Zörgő 30-Dec-12 3:20am
Yes. WMI is a low level infrastructure to access system stuff. Copy-paste the vbs samples and run them to see what they are capable of. And this is a really useful tool to expore wmi data on local or remote system:
Dale 2012 30-Dec-12 3:23am
As an example, the following query selects all the drives on a computer (drive C, D, and so on) that have less than 2 MB of free space:

SELECT * FROM Win32_LogicalDisk WHERE FreeSpace < 2000000

this is pretty much all i need to perform my recursive drive operations on all drives?....
Zoltán Zörgő 30-Dec-12 3:29am
The recursive part in your job, but yes.
Dale 2012 30-Dec-12 3:31am
Ok I will try this thank you very much for your insight I may post back in a few mins and accept this answer
Dale 2012 30-Dec-12 3:39am
ok small problem I have tried the code but "SelectQuery", "ManagementObjectSearcher" and "ManagementObject" are not defined?... what would the VB.Net equivalent to this be?.. I have imported system and system management but the error is still there
Dale 2012 30-Dec-12 3:59am
please help
Dale 2012 30-Dec-12 5:09am
ok sorry for my ignorance I added it to the references and the errors are now gone thank you very much once again
Dale 2012 30-Dec-12 5:53am
For anyone else wishing to do this I will supply the code in efforts to give back a bit

Dim selectQuery As SelectQuery
selectQuery = New SelectQuery("Win32_LogicalDisk")
Dim searcher As ManagementObjectSearcher
searcher = New ManagementObjectSearcher(selectQuery)
Dim disk As ManagementObject
For Each disk In searcher.Get()
Dim WalkHDD As String = disk.ToString
Dim HDDTrim As String = (WalkHDD.Substring(50, WalkHDD.Length - 51)) + "\"

The returned string is only the drive letter which has been trimmed in the code.... if you wish to display the entire string simple delete Dim HDDTrim As String = (WalkHDD.Substring(50, WalkHDD.Length - 51)) + "\"

thank you for helping me to understand this once again

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03-2016 | 2.8.180314.2 | Last Updated 30 Dec 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100