以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  system.management方式获取ip 怎么指定只获取ip4或ip6呢?  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=176782)

--  作者:cnsjroom
--  发布时间:2022/4/26 19:29:00
--  system.management方式获取ip 怎么指定只获取ip4或ip6呢?

添加引用: system.management.dll

帮助文件:https://docs.microsoft.com/zh-cn/windows/win32/cimwin32prov/win32-networkadapterconfiguration

运行如下代码:

Dim query As System.Management.ManagementObjectSearcher = New System.Management.ManagementObjectSearcher("SELECT * FRO M Win32_NetworkAdapterConfiguration")
Dim queryCollection As System.Management.ManagementObjectCollection = query.Get()
Dim mo As New System.Management.ManagementObject
For Each mo In queryCollection
    If IsDBNull(mo.Item("macaddress")) = False Then
        If mo.Item("macaddress") <> "" AndAlso mo.Item("IPEnabled") Then
            output.show(mo.Item("macaddress"))
            output.show(mo.Item("DNSHostName"))
            For Each ip As String In mo.Item("ipaddress")
                output.show(ip)
            Next
        End If
       
    End If
Next

 

 

输出得到:

A0:D3:7A:A5:4E:0C
DESKTOP-4HGMIGM
192.168.43.112
fe80::3da9:7909:ae23:55b9

麻烦老师指导一下  谢谢!

怎么指定只获取ip4或ip6呢?

[此贴子已经被作者于2022/4/26 19:36:32编辑过]

--  作者:有点蓝
--  发布时间:2022/4/26 20:41:00
--  
没接触过ip6,我也不懂。自己看微软的文档吧