2012-11-28 188 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