vb.net 如何获取IP和MAC地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13611142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get IP and MAC address
提问by Vivek Khatri
Possible Duplicate:
How to get my own IP address in C#?
可能的重复:
如何在 C# 中获取我自己的 IP 地址?
My question is as simple as it seems. I want to find the IP address and the MAC address and simply show them in a text box. I am able to get the hostname, but I cant figure out how to get the IP address from it. I am using VB.NET in Visual Studio 2012 (.Net Framework 4.5). The problem is that some of the namespaces in .NET have been changed or moved in visual studio 2012.
我的问题看起来很简单。我想找到 IP 地址和 MAC 地址,然后简单地将它们显示在文本框中。我能够获取主机名,但我不知道如何从中获取 IP 地址。我在 Visual Studio 2012 (.Net Framework 4.5) 中使用 VB.NET。问题是 .NET 中的某些命名空间已在 Visual Studio 2012 中更改或移动。
回答by Rahul Tripathi
Try this:-
尝试这个:-
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;
}
or
或者
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
For getting MAC Address:-
获取 MAC 地址:-
Using mc As New ManagementClass("Win32_NetworkAdapterConfiguration")
For Each mo As ManagementObject In mc.GetInstances()
Console.WriteLine(mo("MacAddress").ToString())
Next
End Using
回答by driis
Get hostname, then IP from the host address list:
从主机地址列表中获取主机名,然后是 IP:
Dim host = Dns.GetHostEntry(Dns.GetHostName())
Dim ip = host.AddressList.FirstOrDefault(Function(x as IPAddress) _
x.AddressFamily = System.Net.Sockets.AddressFamily.Internetwork)
Similarly, you can get the MAC address of one or more network adapters in the machine (example code demonstrates finding the first one that is available):
同样,您可以获取机器中一个或多个网络适配器的 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()
回答by Steven Doggart
First, create a class that can hold all the info you want to return:
首先,创建一个可以保存所有要返回的信息的类:
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
Then, make a method that loops through all the network interfaces and finds the ones that meet your criteria. Then, loop through all the IP addresses for those interfaces until you find one that meets your criteria. Once you find a match, return the info:
然后,创建一个方法来遍历所有网络接口并找到符合您标准的接口。然后,遍历这些接口的所有 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

