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