2012-11-28 39 views
-1

可能重复:
How to get my own IP address in C#?如何获得IP和MAC地址

我的问题是,因为它看起来那么简单。我想查找IP地址和MAC地址,并将其显示在文本框中。我能够获得主机名,但我不知道如何从中获取IP地址。我在Visual Studio 2012(.Net Framework 4.5)中使用VB.NET。问题是,一些在.NET命名空间已被更改或在Visual Studio 2012年移动

+0

有一对夫妇在这里不同的问题,但他们都重复的 - 见[可靠的方法来获得在C#机的MAC地址(http://stackoverflow.com/questions/850650 /可靠的方法,以获取机器MAC地址在C - 锐)和[如何让我自己的IP地址在C#?](http://stackoverflow.com/questions/1069103 /如何到获得 - 我 - 自己的IP地址的,在-C)。这些样本很容易从C#转换为VB.Net – Justin

+0

问题是关于如何在VB.NET中获得MAC和IP地址,所以我不确定为什么这是封闭的,因为它只是一个问题的完全重复关于如何在C#中查找IP地址。这对我来说似乎是一个奇怪的评估。 –

+0

@StevenDoggart - 那么你应该问两个问题 – Mark

回答

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 
+0

问题标记为VB ... – driis

+0

现在是@driis? –

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() 
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