Windows Scripting
  Home arrow Windows Scripting arrow 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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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 / 15
    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


    (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.

    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

    - More Windows Scripting Workarounds from Nilpo
    - Overloading Methods and More in VBScript
    - Improving MFC for Windows Vista
    - Regular Expressions in VBScript
    - Working with Dates in WMI
    - Completing Calendars with VBScript Date Func...
    - Building Calendars with VBScript Date Functi...
    - Working With Dates and Times in VBScript
    - Designing WCF DataContract Classes Using the...
    - Understanding Dates and Times in VBScript
    - Working With Arrays in VBScript
    - Compressed Folders in WSH
    - Using .NET Interops in VBScript
    - Nilpo`s Scripting Secrets, Vol I
    - Database operations using Silverlight 2.0 WC...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek