HOW TO:枚举网络邻居的计算机及其共享文件夹

HOW TO:枚举网络邻居的计算机及其共享文件夹

 

 

 

 

 

 

Author:水如烟(http://blog.csdn.net/lzmtw)

 

 

 

Date:2005-4-26

 

 

 

 

 

 

关键引用空间:System.directoryservices,System.Management

 

 

 

说明:Form上有一个TreeView1,Button1

 

 

 

代码如下:

 

 

 

 

 

 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

 

 

 

        GetNetComputerNameAndShareFolder()

 

 

 

    End Sub

 

 

 

 

 

 

    Private Sub GetNetComputerNameAndShareFolder()

 

 

 

        Dim n As TreeNode = AddNode("工作组或域")

 

 

 

        Dim root As DirectoryEntry = New DirectoryEntry("WinNT:")

 

 

 

        For Each Domain As DirectoryEntry In root.Children

 

 

 

            '枚举工作组或域

 

 

 

            Dim n1 As TreeNode = AddNode(n, Domain.Name)

 

 

 

            For Each Computer As DirectoryEntry In Domain.Children

 

 

 

                '枚举指定工作组或域的计算机

 

 

 

                If Computer.SchemaClassName.Equals("Computer") Then

 

 

 

                    Dim n2 As TreeNode = AddNode(n1, Computer.Name)

 

 

 

                    '枚举指定计算机的共享文件夹

 

 

 

                    Dim o As New ManagementObject

 

 

 

                    o.Scope = New ManagementScope(String.Format("//{0}/root/cimv2", Computer.Name))

 

 

 

                    Dim searcher As ManagementObjectSearcher = New ManagementObjectSearcher("SELECT * FROM Win32_share")

 

 

 

                    searcher.Scope = o.Scope

 

 

 

                    For Each share As ManagementObject In searcher.Get()

 

 

 

                        AddNode(n2, share.GetPropertyValue("Name").ToString)

 

 

 

                    Next

 

 

 

                End If

 

 

 

            Next

 

 

 

        Next

 

 

 

    End Sub

 

 

 

 

 

 

    Private Function AddNode(ByVal Text As String) As TreeNode

 

 

 

        Return Me.TreeView1.Nodes.Add(Text)

 

 

 

    End Function

 

 

 

 

 

 

    Private Function AddNode(ByVal Node As TreeNode, ByVal Text As String) As TreeNode

 

 

 

        Return Node.Nodes.Add(Text)

 

 

 

    End Function

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(HOW,TO,系列)