如何获取IP和MAC地址

时间:2012-11-28 17:39:08

标签: .net vb.net xaml

  

可能重复:
  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中更改或移动。

3 个答案:

答案 0 :(得分:0)

试试这个: -

 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

答案 1 :(得分:0)

从主机地址列表中获取主机名,然后输入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()

答案 2 :(得分:0)

首先,创建一个可以保存您想要返回的所有信息的类:

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