加茂屋株式会社 - ソフトウェアー - Tips


LAN上コンピューター名を列挙する方法。


'VB2005
'Active Directory 階層より、コンピューター名を列挙する方法です。

'参照に System. Services を追加します。
Imports System. Services
Imports System.Net.Dns

Public Class func
    'コンピューターの情報を格納する型を作ります。
    Friend Structure NetComputer
        Friend Name As String
        Friend Path As String
        Friend IPAddress As System.Net.IPAddress()
        Friend ParentPath As String
        Friend ParentName As String
        Friend SchemaClassName As String
    End Structure

    'Active   階層に登録されていれば、起動していなくても、列挙されてしまいます。
    'そこで、IPAddress の取得に失敗したものは、列挙しないオプションのついた、関数を作ってみました。
    '(時間が、かかります。)
    '編集の都合で、改行されているところは、適当に戻してください。
    Friend Shared Function GetCompNameOnLAN(ByVal withIPAddress As Boolean) As NetComputer()
        Dim NetComputerA(-1) As NetComputer
        Dim RootEntry As New  Entry
        Dim ChildEntry As  Entry
        Dim NestRootEntry As  Entry
        Dim NestChildEntry As  Entry
        Dim IPAddressA As System.Net.IPAddress()
        RootEntry.Path = "WinNT:"
        For Each ChildEntry In RootEntry.Children
            If ChildEntry.SchemaClassName.ToLower = "domain" Then
                ReDim Preserve NetComputerA(NetComputerA.GetUpperBound(0) + 1)
                NetComputerA(NetComputerA.GetUpperBound(0)).IPAddress = Nothing
                NetComputerA(NetComputerA.GetUpperBound(0)).Name = ChildEntry.Name
                NetComputerA(NetComputerA.GetUpperBound(0)).Path = ChildEntry.Path
                NetComputerA(NetComputerA.GetUpperBound(0)).ParentName = ChildEntry.Parent.Name
                NetComputerA(NetComputerA.GetUpperBound(0)).ParentPath = ChildEntry.Parent.Path
                NetComputerA(NetComputerA.GetUpperBound(0)).SchemaClassName = ChildEntry.SchemaClassName
                NestRootEntry = New  Entry
                NestRootEntry.Path = "WinNT://" & ChildEntry.Name
                For Each NestChildEntry In NestRootEntry.Children
                    If NestChildEntry.SchemaClassName.ToLower = "computer" Then
                        Try
                            If withIPAddress Then
                                IPAddressA = GetHostAddresses(NestChildEntry.Name)
                            Else
                                IPAddressA = Nothing
                            End If
                            ReDim Preserve NetComputerA(NetComputerA.GetUpperBound(0) + 1)
                            NetComputerA(NetComputerA.GetUpperBound(0)).Name = NestChildEntry.Name
                            NetComputerA(NetComputerA.GetUpperBound(0)).Path = NestChildEntry.Path
                            NetComputerA(NetComputerA.GetUpperBound(0)).ParentName = NestChildEntry.Parent.Name
                            NetComputerA(NetComputerA.GetUpperBound(0)).ParentPath = NestChildEntry.Parent.Path
                            NetComputerA(NetComputerA.GetUpperBound(0)).IPAddress = IPAddressA
                            NetComputerA(NetComputerA.GetUpperBound(0)).SchemaClassName = NestChildEntry.SchemaClassName
                        Catch ex As Exception
                        End Try
                    End If
                Next
            End If
        Next
        Return NetComputerA
    End Function
End Class

Tipsへ


お問い合わせは、こちら

加茂屋株式会社 (C) Kamoya Corporation