Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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
Dale 20121.8K

1 solution

Rate this: bad
good
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: http://msdn.microsoft.com/en-us/library/windows/desktop/aa394592(v=vs.85).aspx[^]. The samples are in vbs, but they can be easily transcribed to vb.net. Here you have some good examples about querying wmi in vb.net: http://msdn.microsoft.com/en-us/library/ms186146(v=vs.100).aspx?cs-lang=vb[^].
  Permalink  
v2
Comments
Dale 2012 at 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ő at 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: http://www.ks-soft.net/hostmon.eng/wmi/index.htm
Dale 2012 at 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ő at 30-Dec-12 3:29am
   
The recursive part in your job, but yes.
Dale 2012 at 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 at 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 at 30-Dec-12 3:59am
   
please help
Dale 2012 at 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 at 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)) + "\"
Next
 
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
0 OriginalGriff 449
1 Maciej Los 274
2 CPallini 160
3 Sergey Alexandrovich Kryukov 152
4 Richard MacCutchan 130
0 OriginalGriff 5,474
1 Sergey Alexandrovich Kryukov 4,713
2 Peter Leow 2,944
3 DamithSL 2,465
4 Maciej Los 2,434


Advertise | Privacy | Mobile
Web01 | 2.8.140718.1 | Last Updated 30 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid