Working with Drives/Files/Folders using WMI and Visual Basic.NET
(Page 1 of 5 )
This article explains how to retrieve folder/file/drive information dynamically using Visual Basic.NET and VBScript.
A downloadable file for this article is available
here.
The sample downloadable solution (zip) is entirely developed using Visual Studio.NET 2003 Enterprise Architect on Windows Server 2003 Standard Edition. But, I am confident that it would work with other versions of Windows (which support .NET 1.1) as well.
I contributed several articles previously about WMI with VB.NET and VBScript (including some on introductory or basic topics of WMI). I even contributed a series (of about six articles) about “WMI Programming on VB.NET” covering several aspects of WMI. I strongly suggest you go through that series, before going through this article.
How to list all disk drives using Visual Basic.NET
Before getting out the information on drives available in on your computer, we need to create a wrapper to store the same information. Let us proceed with creating a wrapper:
Public Function getDiskDriveStructure() As DataTable
Dim dt As New DataTable
dt.Columns.Add(New DataColumn("Caption"))
dt.Columns.Add(New DataColumn("DeviceID"))
dt.Columns.Add(New DataColumn("Partitions"))
dt.Columns.Add(New DataColumn("Size"))
Return dt
End Function
The following method “addDiskDrive” adds a single row based on the structure you create for the data table using the above method.
Public Sub addDiskDrive(ByRef dt As DataTable, ByVal Caption As String, ByVal DeviceID As String, ByVal Partitions As String, ByVal Size As String)
Dim dr As DataRow
dr = dt.NewRow
dr("Caption") = Caption
dr("DeviceID") = DeviceID
dr("Partitions") = Partitions
dr("Size") = Size
dt.Rows.Add(dr)
End Sub
After creating the wrapper, the following VB.NET code should support some minimum information about the “drives” available in your system.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim searcher As New ManagementObjectSearcher( _
"root\CIMV2", _
"SELECT * FROM Win32_DiskDrive")
Dim dt As DataTable = globals.getDirectoryStructure
For Each queryObj As ManagementObject In searcher.Get()
globals.addDirectory(dt, queryObj("Caption"), queryObj("DeviceID"), Convert.ToString(queryObj("Partitions")), Convert.ToString(queryObj("Size")))
Next
Me.DataGrid1.DataSource = dt
Catch err As ManagementException
MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
End Try
End Sub
You can achieve the same with VBScript as follows:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_DiskDrive",,48)
For Each objItem in colItems
Wscript.Echo "Caption: " & objItem.Caption
Wscript.Echo "DeviceID: " & objItem.DeviceID
Wscript.Echo "Partitions: " & objItem.Partitions
Wscript.Echo "Size: " & objItem.Size
Next
If you include “DriveType=3” in the query, it will return results only for hard disks.
Next: How to list all logical disk drives using Visual Basic.NET >>
More Windows Scripting Articles
More By Jagadish Chaterjee