如何获取IP和MAC地址[重复]
Posted
技术标签:
【中文标题】如何获取IP和MAC地址[重复]【英文标题】:How to get IP and MAC address [duplicate] 【发布时间】:2012-11-16 15:53:52 【问题描述】:可能重复:How to get my own IP address in C#?
我的问题看起来很简单。我想找到 IP 地址和 MAC 地址,然后在文本框中简单地显示它们。我能够获取主机名,但我不知道如何从中获取 IP 地址。我在 Visual Studio 2012 (.Net Framework 4.5) 中使用 VB.NET。问题是 .NET 中的某些命名空间已在 Visual Studio 2012 中更改或移动。
【问题讨论】:
这里有几个不同的问题,但它们都是重复的 - 请参阅 Reliable method to get machine's MAC address in C# 和 How to get my own IP address in C#?。示例很容易从 C# 转换为 VB.Net 问题是关于如何在 VB.NET 中同时获取 MAC 和 IP 地址,所以我不知道为什么这被关闭,因为它与一个关于如何查找的问题完全重复C# 中的 IP 地址。这对我来说似乎是一个奇怪的评估。 @StevenDoggart - 那么你应该问两个问题 @Mark 好吧,这不是它被关闭的原因,但即便如此,我认为这不是两个独立的问题,因为找到同一网络接口的 MAC 地址和 IP 地址不是一定很明显。 【参考方案1】:首先,创建一个可以包含所有您想要返回的信息的类:
Public Class NetworkInterfaceInfo
Public Sub New(ByVal ipAddress As IPAddress, ByVal physicalAddress As PhysicalAddress)
_ipAddress = ipAddress
_physicalAddress = physicalAddress
End Sub
Public ReadOnly Property IpAddress() As IPAddress
Get
Return _ipAddress
End Get
End Property
Private _ipAddress As IPAddress
Public ReadOnly Property PhysicalAddress() As PhysicalAddress
Get
Return _physicalAddress
End Get
End Property
Private _physicalAddress As PhysicalAddress
End Class
然后,创建一个遍历所有网络接口并找到满足您条件的方法的方法。然后,遍历这些接口的所有 IP 地址,直到找到符合条件的 IP 地址。找到匹配项后,返回信息:
Public Function GetNetworkInterfaceInfo() As NetworkInterfaceInfo
For Each networkInterface As NetworkInterface In networkInterface.GetAllNetworkInterfaces()
If networkInterface.OperationalStatus = OperationalStatus.Up Then
For Each address As IPAddress In networkInterface.GetIPProperties().DnsAddresses()
If address.AddressFamily = AddressFamily.InterNetwork Then
Return New NetworkInterfaceInfo(address, networkInterface.GetPhysicalAddress())
End If
Next
End If
Next
Return Nothing
End Function
【讨论】:
【参考方案2】:试试这个:-
public string GetLocalIP()
string _IP = null;
System.Net.IPHostEntry _IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());
foreach (System.Net.IPAddress _IPAddress in _IPHostEntry.AddressList)
if (_IPAddress.AddressFamily.ToString() == "InterNetwork")
_IP = _IPAddress.ToString();
return _IP;
或
Try
Dim IpCollection As New Collection
Dim i As Integer
Dim ipE As Net.IPHostEntry = System.Net.Dns.GetHostEntry(-HOSTNAME-)
Dim IpA() As Net.IPAddress = ipE.AddressList
For i = 0 To IpA.GetUpperBound(0)
IpCollection.Add(IpA(i).ToString)
Next
Dim Ipaddress As String
Ipaddress = IpCollection.GetValue(-Num-)
Catch ex As Exception
MsgBox("An error has occured")
End Try
获取MAC地址:-
Using mc As New ManagementClass("Win32_NetworkAdapterConfiguration")
For Each mo As ManagementObject In mc.GetInstances()
Console.WriteLine(mo("MacAddress").ToString())
Next
End Using
【讨论】:
【参考方案3】:从主机地址列表中获取主机名,然后是 IP:
Dim host = Dns.GetHostEntry(Dns.GetHostName())
Dim ip = host.AddressList.FirstOrDefault(Function(x as IPAddress) _
x.AddressFamily = System.Net.Sockets.AddressFamily.Internetwork)
同样,您可以获取机器中一个或多个网络适配器的 MAC 地址(示例代码演示如何找到第一个可用的):
Dim networkInterface = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()
Dim firstNetwork = networkInterface.FirstOrDefault(Function(x as System.Net.NetworkInformation.NetworkInterface) _
x.OperationalStatus = System.Net.NetworkInformation.OperationalStatus.Up)
Dim firstMacAddressOfWorkingNetworkAdapter = firstNetwork.GetPhysicalAddress()
【讨论】:
以上是关于如何获取IP和MAC地址[重复]的主要内容,如果未能解决你的问题,请参考以下文章
获取访问我的站点的客户端的 MAC 地址和 IP 地址 [重复]
如何在 laravel 中获取客户端 IP 地址和 MAC 地址?