Windows Scripting
  Home arrow Windows Scripting arrow Page 3 - 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 
Mobile Linux 
App Generation ROI 
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 / 12
    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 folders/files information (including sub-folders) using Visual Basic.NET


    (Page 3 of 5 )

    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()
                    globals.addDirectory(dt, queryObj("FileName"), queryObj("Path"), queryObj("Readable"), queryObj("Writeable"))
                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_Directory",,48)
    For Each objItem in colItems
        Wscript.Echo "FileName: " & objItem.FileName
        Wscript.Echo "Path: " & objItem.Path
        Wscript.Echo "Readable: " & objItem.Readable
        Wscript.Echo "Writeable: " & objItem.Writeable
    Next

    You can get the list of files by replacing “Win32_directory” with “CIM_DataFile.”

    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

    - Introducing Two-Way Data Binding using Silve...
    - Silverlight 2.0 Application Development with...
    - Burning Multisession CDs with IMAPI2 in WSH
    - Creating a Silverlight 2.0 Application that ...
    - Burning CDs with the IMAPI2 Control
    - Burning CDs in Windows XP with WSH
    - Advanced Word Object Scripting
    - Reading and Printing Word Documents in WSH
    - 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+





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