0

可能重复:
如何在 C# 中获取我自己的 IP 地址?

我的问题看起来很简单。我想找到 IP 地址和 MAC 地址,然后在文本框中简单地显示它们。我能够获取主机名,但我不知道如何从中获取 IP 地址。我在 Visual Studio 2012 (.Net Framework 4.5) 中使用 VB.NET。问题是 .NET 中的某些命名空间已在 Visual Studio 2012 中更改或移动。

4

3 回答 3

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
于 2012-11-28T17:42:15.640 回答
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()
于 2012-11-28T17:43:20.250 回答
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
于 2012-11-28T17:48:57.893 回答