Windows Scripting
  Home arrow Windows Scripting arrow Page 5 - 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 
Dedicated Servers 
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 delete a folder using Visual Basic.NET


    (Page 5 of 5 )

    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("Delete")
                Dim outParams As ManagementBaseObject = _
                    classInstance.InvokeMethod("Delete", 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_("Delete"). _
        inParameters.SpawnInstance_()
     
    Set objOutParams = objWMIService.ExecMethod("Win32_Directory.Name=' d:\DataAccessLibrary '", "Delete", objInParam)
     
    Wscript.Echo "Out Parameters: "
    Wscript.echo "ReturnValue: " & objOutParams.ReturnValue

    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.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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

    - 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
    - Screen Capturing via GDI+ and GDI
    - Understanding Procedures in VBScript
    - Printing Documents in WSH
    - Generating Outlook Signatures Based on Activ...
    - VBScript: Converting and Formatting with Fun...
    - VBScript: Conversion and Format Functions
    - VBScript: Array Functions
    - VBScript: Strings, You Can`t Function withou...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway