网络发现计划

时间:2011-08-22 21:05:05

标签: vb.net

我正在尝试理解和修改网络发现程序..如果有人帮助我理解以下代码我将被迫

Dim DomainEntry As New DirectoryEntry("WinNT://" & workGroup.Trim())
            DomainEntry.Children.SchemaFilter.Add("computer")
            X = 5 : Y = 5 : Count = 1
            For Each Machine As DirectoryEntry In DomainEntry.Children
                Dim CompNode As New TreeNode(), CompInfo(1) As String
                CompInfo(0) = Machine.Name
                Dim Tempaddr As System.Net.IPHostEntry = Nothing
                Try
                    Tempaddr = DirectCast(Dns.GetHostByName(Machine.Name), System.Net.IPHostEntry)
                    Dim TempAd As System.Net.IPAddress() = Tempaddr.AddressList, str As String = ""
                    For Each TempA As IPAddress In TempAd
                        CompInfo(1) = TempA.ToString()
                    Next

                Catch ex As Exception
                    CompInfo(1) = ""
                End Try

2 个答案:

答案 0 :(得分:1)

希望这会有所帮助:

''//The variable "workGroup" holds your Active Directory domain name
''//The "DomainEntry" variable will represent the root of your Active Directory hierarchy
Dim DomainEntry As New DirectoryEntry("WinNT://" & workGroup.Trim())
''//Tell the "DomainEntry" variable to only look at "computer" objects
DomainEntry.Children.SchemaFilter.Add("computer")
''//These variables are not used
X = 5 : Y = 5 : Count = 1
''//Loop through all of the computers in the domain
For Each Machine As DirectoryEntry In DomainEntry.Children
    ''//First variable is not used, second is an array with two parts
    Dim CompNode As New TreeNode(), CompInfo(1) As String
    ''//Set the first part of the array to the machine name
    CompInfo(0) = Machine.Name
    ''//The next block tries to get the machine IP by looking it up in DNS. It can fail at several points so it gets wrapped in a try/catch just in case
    Dim Tempaddr As System.Net.IPHostEntry = Nothing
    Try
        ''//Try getting the machine IP
        Tempaddr = DirectCast(Dns.GetHostByName(Machine.Name), System.Net.IPHostEntry)
        ''//A machine can have several IP addresses so this gets a full list of them
        Dim TempAd As System.Net.IPAddress() = Tempaddr.AddressList, str As String = ""
        ''//Most machines will probably have just one IP address, but just in case this code takes the last one that it finds
        For Each TempA As IPAddress In TempAd
            ''//Set the second part of our array to the IP address
            CompInfo(1) = TempA.ToString()
        Next
    Catch ex As Exception
        ''//If this is hit then there was a problem getting the IP address so set it to blank
        CompInfo(1) = ""
    End Try
Next

答案 1 :(得分:0)

这是一个用于读取本地Active Directory的程序。 “WinNT:// workgroup”是目录的地址。

下一行

DomainEntry.Children.SchemaFilter.Add("computer")

表示您只对“计算机”类型的项目感兴趣。从那时起,您将创建一个二维数组,表示第零个元素中具有机器名称的计算机,以及第二个中的机器的IP地址。如果机器有多个IP条目,则该数组将包含最后一个。