2011-08-22 56 views
0

餘米試圖理解和修改網絡發現program..if有人幫助我理解下面的代碼,我會不得不網絡發現計劃

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 
+0

你會好得多詢問的代碼,你不懂行的問題。就目前而言,你的問題非常廣泛。 –

+0

但如果我不明白這整個代碼塊:-o – Madiha

+0

DomainEntry.Children.SchemaFilter.Add(「計算機」)專門這一行! – Madiha

回答

0

這是用於讀取本地Active Directory的程序。 「WinNT://工作組」是目錄的地址。

下一行

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

表明您只對類型「計算機」的項目感興趣。從那裏開始,您將創建一個二維數組,它表示第零個元素中包含機器名稱的計算機,以及第二個元素中機器的IP地址。如果機器有多個IP條目,則數組將包含最後一個。

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