Windows Scripting
  Home arrow Windows Scripting arrow Page 4 - 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 retrieve the sub-folder information of a particular folder using Visual Basic.NET


    (Page 4 of 5 )

    I am trying to use the same wrapper I created in the previous section.  So, let us directly proceed with the code.

    Try
                Dim searcher As New ManagementObjectSearcher( _
                       "root\CIMV2", _
                       "Associators of {Win32_Directory.Name='d:\DataAccessLibrary'} " _
                               & "Where AssocClass =
    Win32_Subdirectory " _
                                & "ResultRole = PartComponent")
     
                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

    You can achieve the same with VBScript as follows:

    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
    Set colItems = objWMIService.ExecQuery( _
        "Associators of {Win32_Directory.Name='d:\DataAccessLibrary'} " _
                               & "Where AssocClass = Win32_Subdirectory " _
                                & "ResultRole = PartComponent",,48)
    For Each objItem in colItems
        Wscript.Echo "Description: " & objItem.Description
        Wscript.Echo "ProxyPortNumber: " & objItem.ProxyPortNumber
        Wscript.Echo "ProxyServer: " & objItem.ProxyServer
        Wscript.Echo "ServerName: " & objItem.ServerName
    Next

    How to rename a folder using Visual Basic.NET

    As I am operating against a particular WMI object, we do not need a wrapper for this.  We are not retrieving any information at this point.

    The following code renames the folder you specify.

    Try
                Dim classInstance As New ManagementObject( _
                    "root\CIMV2", _
                    "Win32_Directory.Name='d:\DataAccessLibrary'", _
                    Nothing)
                Dim inParams As ManagementBaseObject = _
                    classInstance.GetMethodParameters("Rename")
                inParams("FileName") = "d:\DataAccessLibrary2"
                Dim outParams As ManagementBaseObject = _
                    classInstance.InvokeMethod("Rename", inParams, Nothing)
                MessageBox.Show(outParams("ReturnValue"))
            Catch err As ManagementException
                MessageBox.Show("An error occurred while trying to execute the WMI method: " & err.Message)
            End Try

    You can achieve the same with VBScript as follows:

    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
    Set objShare = objWMIService.Get("Win32_Directory.Name='d:\DataAccessLibrary'")
     
    Set objInParam = objShare.Methods_("Rename"). _
        inParameters.SpawnInstance_()
     
     
    objInParam.Properties_.Item("FileName") =  "d:\DataAccessLibrary2"
     
    Set objOutParams = objWMIService.ExecMethod("Win32_Directory.Name=' d:\DataAccessLibrary '", "Rename", objInParam)
     
    Wscript.Echo "Out Parameters: "
    Wscript.echo "ReturnValue: " & objOutParams.ReturnValue

    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 3 hosted by Hostway
    Stay green...Green IT