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()
Before getting the information on all the logical drives available on your computer, we need to create a wrapper to store the same information. Let us proceed with creating a wrapper:
Public Function getLogicalDriveStructure() As DataTable
Dim dt As New DataTable
dt.Columns.Add(New DataColumn("Description"))
dt.Columns.Add(New DataColumn("DeviceID"))
dt.Columns.Add(New DataColumn("FileSystem"))
dt.Columns.Add(New DataColumn("FreeSpace"))
dt.Columns.Add(New DataColumn("Name"))
dt.Columns.Add(New DataColumn("Size"))
Return dt
End Function
The following method “addLogicalDrive” adds a single row based on the structure you create for the data table using the above method.
Public Sub addLogicalDrive(ByRef dt As DataTable, ByVal Description As String, ByVal DeviceID As String, ByVal FileSystem As String, ByVal FreeSpace As String, ByVal Name As String, ByVal Size As String)
Dim dr As DataRow
dr = dt.NewRow
dr("Description") = Description
dr("DeviceID") = DeviceID
dr("FileSystem") = FileSystem
dr("FreeSpace") = FreeSpace
dr("Name") = Name
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 logical 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_LogicalDisk")
Dim dt As DataTable = globals.getLogicalDriveStructure
For Each queryObj As ManagementObject In searcher.Get()
Before getting the information on folders available on your computer, we need to create a wrapper to store the same information. Let us proceed with creating a wrapper:
Public Function getDirectoryStructure() As DataTable
Dim dt As New DataTable
dt.Columns.Add(New DataColumn("FileName"))
dt.Columns.Add(New DataColumn("Path"))
dt.Columns.Add(New DataColumn("Readable"))
dt.Columns.Add(New DataColumn("Writeable"))
Return dt
End Function
The following method “addDirectory” adds a single row based on the structure you create for the data table using the above method.
Public Sub addDirectory(ByRef dt As DataTable, ByVal FileName As String, ByVal Path As String, ByVal Readable As String, ByVal Writeable As String)
Dim dr As DataRow
dr = dt.NewRow
dr("FileName") = FileName
dr("Path") = Path
dr("Readable") = Readable
dr("Writeable") = Writeable
dt.Rows.Add(dr)
End Sub
After creating the wrapper, the following VB.NET code should support some minimum information about the “folders” 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_Directory")
Dim dt As DataTable = globals.getDirectoryStructure
For Each queryObj As ManagementObject In searcher.Get()
All the above topics are only simple programs to get you started immediately. You can still find much more information on every file/folder/drive using several other properties. You can also use several operations other than just deleting and renaming. I suggest you check MSDN for further information about properties, methods, and so on related to WMI.
Any comments, suggestions, bugs, errors, feedback etc. are highly appreciated at jag_chat@yahoo.com.