Listing folders on remote computer using wmi

G

Guest

I am trying to list the sub folders in a folder on a remote computer. I have
found some samples in C# but cannot get it to work in VB.NET. I get a
"specified cast is not valid exception".

selectQuery = New WqlObjectQuery("ASSOCIATORS OF
{Win32_Directory.Name=""c:\\""} WHERE AssocClass = Win32_Subdirectory
ResultRole = PartComponent")

What I am doing wrong?
 
S

Steven Cheng[MSFT]

Hi ,

Thanks for your posting. As for the WMI query problem you mentioned, it
seems due to some type casting error based on the error message. I think we
can do the following things first:

Test the wmi query statment in vb or vbscript code to see whether it can
work correctly so as to verify that the code is ok. Then, if you feel it
convenient , you may provide some further code snippet on your vbnet app so
that we can do some further tests on ourside.

Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
G

Guest

Hi Stephen,

I wrote a the routines to iterate through folders on a remote computer in
C#. I took the code and converted it VB.NET. It works. I took the exact
same syntax and plugged it into my new VB.NET class and the statement works.
I am not sure what is different. I am posting the code to my VB.NET class
for others to use as a sample since I have found very little documentation on
the .NET side.

Imports System.Management

Public Class FileSystem
Private moScope As ManagementScope

Public Sub New(ByVal sComputerName As String)
moScope = New ManagementScope("\\" + sComputerName + "\root\cimv2")
End Sub

Public Function RetrieveFixedDrives() As String()
Dim oSelectQuery As SelectQuery
Dim iDriveType As Integer
Dim sDrives() As String
Dim oSearcher As ManagementObjectSearcher
Dim oMgmtObjColl As ManagementObjectCollection
Dim oDisk As ManagementObject
Dim iCount As Integer

oSelectQuery = New SelectQuery("win32_logicalDisk")
oSearcher = New ManagementObjectSearcher(moScope, oSelectQuery)

oMgmtObjColl = oSearcher.Get
ReDim sDrives(oMgmtObjColl.Count)

iCount = 0

For Each oDisk In oMgmtObjColl
iDriveType =
Integer.Parse(oDisk.Properties("DriveType").Value.ToString)
If iDriveType = 3 Then
sDrives(iCount) =
oDisk.Properties("DeviceID").Value.ToString()
iCount += 1
End If
Next oDisk

ReDim Preserve sDrives(iCount - 1)

Return sDrives
End Function

Public Function RetrieveFolders(ByVal sParentFolder As String) As String()
Dim sFolders() As String
Dim sReturnFolders() As String
Dim iCount As Integer
Dim oMgmtObjColl As ManagementObjectCollection
Dim oFolder As ManagementObject
Dim bHidden As Boolean
Dim oSelectQuery As New WqlObjectQuery("ASSOCIATORS OF
{Win32_Directory.Name='" + sParentFolder + "'} WHERE AssocClass =
Win32_Subdirectory ResultRole = PartComponent")
Dim oSearcher As New ManagementObjectSearcher(moScope, oSelectQuery)

oMgmtObjColl = oSearcher.Get

ReDim sFolders(oMgmtObjColl.Count)
iCount = 0

For Each oFolder In oSearcher.Get()

bHidden = Convert.ToBoolean(oFolder.Properties("Hidden").Value)
If bHidden = False Then

sFolders(iCount) =
oFolder.Properties("FileName").Value.ToString()
iCount += 1
End If

Next

ReDim Preserve sFolders(iCount - 1)

Return sFolders
End Function

End Class
 
S

Steven Cheng[MSFT]

Hi ,

Thanks for your followup.
I'm glad that you've got the code work correctly. Also, thanks again for
sharing your code with the communitry as that'll save a lot time for some
others who are working on the same things.
If there is any further problem in the future, please feel free to post
here.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top