0

Possible Duplicate:
How to get my own IP address in C#?

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.

Community
  • 1
  • 1
Vivek Khatri
  • 51
  • 1
  • 1
  • 7
  • There are a couple of different questions here but they are all duplicates - see [Reliable method to get machine's MAC address in C#](http://stackoverflow.com/questions/850650/reliable-method-to-get-machines-mac-address-in-c-sharp) and [How to get my own IP address in C#?](http://stackoverflow.com/questions/1069103/how-to-get-my-own-ip-address-in-c). The samples are easily converted from C# to VB.Net – Justin Nov 28 '12 at 17:41
  • The question is about how to get both the MAC and the IP addresses in VB.NET, so I'm not sure why this was closed as being an exact duplicate of a question just about how to find the IP address in C#. That seems like an odd assessment to me. – Steven Doggart Nov 29 '12 at 12:07
  • @StevenDoggart - then you should ask two questions – mmmmmm Nov 29 '12 at 12:26
  • @Mark Well, that's not the given reason why it was closed, but even so, I don't think it's two separate questions because finding the MAC address and IP address for the same network interface is not necessarily obvious. – Steven Doggart Nov 29 '12 at 12:45

3 Answers3

0

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:-

  Using mc As New ManagementClass("Win32_NetworkAdapterConfiguration")
For Each mo As ManagementObject In mc.GetInstances()
    Console.WriteLine(mo("MacAddress").ToString())
Next
   End Using
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

Get hostname, then IP from the host address list:

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

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()
driis
  • 161,458
  • 45
  • 265
  • 341
0

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:

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
Steven Doggart
  • 43,358
  • 8
  • 68
  • 105