如何获取计算机的网卡名和相应的IP地址?
如何获取计算机的网卡名和相应的IP地址?即用IPCONFIG获到的信息相同 问题点数:0、回复次数:4Top
1 楼mingfei200169(木目)回复于 2004-08-05 23:01:50 得分 0
网卡ID 号在标准的.NET命名空间中是无法获得的,必须用P/Invoke调用一个win32API方法来找到它,获得相应的IP地址
Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Text.RegularExpressions
Imports Microsoft.VisualBasic
Module M_TestIPAddress
Class TestIPAddress
'通过dns主机名获得IP地址信息
Private Shared Sub IPAddresses(ByVal server As String)
Try
Dim ASCII As New System.Text.ASCIIEncoding
' Get server related information.
Dim heserver As IPHostEntry = Dns.Resolve(server)
' Loop on the AddressList
Dim curAdd As IPAddress
For Each curAdd In heserver.AddressList
' Display the type of address family supported by the server. If the
' server is IPv6-enabled this value is: InternNetworkV6. If the server
' is also IPv4-enabled there will be an additional value of InterNetwork.
Console.WriteLine(("AddressFamily: " + curAdd.AddressFamily.ToString()))
' Display the ScopeId property in case of IPV6 addresses.
If curAdd.AddressFamily.ToString() = ProtocolFamily.InterNetworkV6.ToString() Then
Console.WriteLine(("Scope Id: " + curAdd.ScopeId.ToString()))
End If
'显示标准格式的ip地址
Console.WriteLine(("Address: " + curAdd.ToString()))
Console.WriteLine(ControlChars.CrLf)
Next curAdd
Catch e As Exception
Console.WriteLine(("[DoResolve] Exception: " + e.ToString()))
End Try
End Sub 'IPAddresse
Public Shared Sub Main(ByVal args() As String)
Dim server As String = Nothing
' Define a regular expression to parse user's input.
' This is a security check. It allows only
' alphanumeric input string between 2 to 40 character long.
'Define a regular expression to parse user's input.
'This is a security check. It allows only
'alphanumeric input string between 2 to 40 character long.
Dim rex As New Regex("^[a-zA-Z]\w{1,39}$")
If args.Length < 1 Then
' If no server name is passed as an argument to this program, use the current
' server name as default.
server = Dns.GetHostName() '获得DNS主机名
Console.WriteLine(("Using current host: " + server))
Else
server = args(0)
If Not rex.Match(server).Success Then
Console.WriteLine("Input string format not allowed.")
Return
End If
End If
' Get the list of the addresses associated with the requested server.
IPAddresses(server)
' Get additonal address information.
IPAddressAdditionalInfo()
Console.Read()
End Sub 'Main
End Class 'TestIPAddress
End Module
Top
2 楼wahahasnail(笑东笑西笑南笑北笑来笑去笑自己原来无知无识)回复于 2004-08-06 10:00:33 得分 0
用WMI处理
Top
3 楼koomis()回复于 2005-03-26 16:08:43 得分 0
Dim mc As System.Management.ManagementClass = New System.Management.ManagementClass("Win32_NetworkAdapterConfiguration")
Dim moc As System.Management.ManagementObjectCollection = mc.GetInstances()
Dim mo As System.Management.ManagementObject
For Each mo In moc
If CBool(mo("IPEnabled")) = True Then
MessageBox.Show(mo("MacAddress").ToString()) '//---网卡MAC地址
MessageBox.Show(mo("IPAddress")(0).ToString()) '//---网卡IP地址
End If
Next
Top
4 楼daisi(腾飞天涯)回复于 2005-03-26 17:05:36 得分 0
楼上正解Top




