Windows Scripting
  Home arrow Windows Scripting arrow Page 2 - Working with Drives/Files/Folders using WM...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Working with Drives/Files/Folders using WMI and Visual Basic.NET
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 11
    2005-12-28

    Table of Contents:
  • Working with Drives/Files/Folders using WMI and Visual Basic.NET
  • How to list all logical disk drives using Visual Basic.NET
  • How to list all folders/files information (including sub-folders) using Visual Basic.NET
  • How to retrieve the sub-folder information of a particular folder using Visual Basic.NET
  • How to delete a folder using Visual Basic.NET

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Working with Drives/Files/Folders using WMI and Visual Basic.NET - How to list all logical disk drives using Visual Basic.NET


    (Page 2 of 5 )

    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()
                    globals.addLogicalDrive(dt, queryObj("Description"), queryObj("DeviceID"), queryObj("FileSystem"), Convert.ToString(queryObj("FreeSpace")), queryObj("Name"), 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_LogicalDisk",,48)
    For Each objItem in colItems
        Wscript.Echo "Description: " & objItem.Description
        Wscript.Echo "DeviceID: " & objItem.DeviceID
        Wscript.Echo "FileSystem: " & objItem.FileSystem
        Wscript.Echo "FreeSpace: " & objItem.FreeSpace
        Wscript.Echo "Name: " & objItem.Name
        Wscript.Echo "Size: " & objItem.Size
    Next

    More Windows Scripting Articles
    More By Jagadish Chaterjee


       · We hope you found this article enjoyable and educational. Please feel free to...
       · After days of wading through the MSDN doc, and searching for a WMI vb.net tutorial,...
       · Thanks, I'm glad the article was so helpful! Do check out other articles by this...
     

    WINDOWS SCRIPTING ARTICLES

    - Scripting Microsoft Word
    - Using WSH to Catalog MP3 Files
    - Reading MP3 ID3 Tags in WSH
    - A Brief Look at Menus in WPF
    - More Examples of Simplified Image Processing...
    - Completing a WPF To-Do List Application
    - Simplified Image Processing in GDI+
    - Data Converstion and Task Addition with WPF
    - Adding Controls to an Application with WPF
    - A Portable Scripting Toolbox
    - WPF Through an Example: Introduction
    - Beginning SharePoint Web Part Development
    - More Alternative Languages for WSH
    - WPF Control Layout
    - WSH in Other Languages





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT