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
Next: How to delete a folder using Visual Basic.NET >>
More Windows Scripting Articles
More By Jagadish Chaterjee