How to Get System IP Address Using C# – Windows ASP.NET Core Hosting 2024 | Review and Comparison

When it comes to networking, IP addresses are a must-have. While it may not be as critical from a C# perspective, we will look about the System as a result of this. There’s a lot of .NET namespace involved in here.

We may only needed the IP address of a local device. So check the article below to see how we can Get IP address of the system using C# or VB.NET.

Get IP Address in .NET

It’s important to know whose IP address we’re looking for in order to get the IP address we need.

Ipconfig is the command that can be used to find your IP address. To get the basic network information like host name, IP address, and gateway, you can run this command on your Command Prompt in administrator mode.

We have few System.Net classes to deal with this system IP address. Hence, first need to use the namespace System.Net.

The GetHostName() method can be used to obtain the machine name (or host name) from system. This is a part of the DNS class, as well. The IP address of the Host is now available.

The GetHostByName() method and the AddressList array are required for this. GetHostByName’s argument is “host name,” and so on.

Best ASP.NET Hosting

Get Client IP Address In ASP.NET

The IP address of your client can be obtained by calling Request.UserHostAddress in ASP.NET.

We get multiple IP addresses if you use a Windows application to get your local IP address. The netid part of an IP address can be compared to any other IP address in order to find a specific IP address you know.

var addressListArray = Dns.GetHostAddresses(Dns.GetHostName()
if (addressListArray).Length > 0)
{
   ipAddress = addressListArray[0].ToString();
}

Get IP Address Using C# Winforms – Sample Code

Dns.GetHostEntry(Dns.GetHostName()).AddressList
    .Where(ips => !ips.IsIPv6LinkLocal && !ips.IsIPv6Multicast && !ips.IsIPv6SiteLocal)
    .First()
    .ToString();

Get IP Address Using VB.NET – Sample Code

Sub Main(args As String())
  Dim hosts() As systemIpAddresses = Dns.GetHostAddresses(Dns.GetHostName())
  For Each ipAddress As  systemIpAddresses  In hosts
     If ipAddress.AddressFamily = AddressFamily.InterNetwork Then
            Console.WriteLine($"IP {ipAddress} is IPv4")
     End If
  Next
End Sub

FAQ on Get System IP .NET

How to Find Client IP address in ASP.NET ?

Apart from the snippet explained above in the article for getting the system IP address, we can use the two HTTP header fields HTTP_X_FORWARDED_FOR and REMOTE_ADDR for obtaining the IP address of the client connected to internet.

HTTP_X_FORWARDED_FOR – gets the originating IP Address of the client

REMOTE_ADDR – gets IP address of the remote machine connected

    var ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];  
  
    if (string.IsNullOrEmpty(ipAddress))  
    { 
       ipAddress = Request.ServerVariables["REMOTE_ADDR"];  
    } 

How do I find my host name C#?

Make a console application in C# and use the below procedure to get IP address using C#

static void Main(string[] args)
{
  //find host name in C#
  string myHost= Dns.GetHostName(); 
  Console.WriteLine( myHost ); 

  //write the system IP 
  Console.WriteLine(Dns.GetHostByName(myHost).AddressList[0].ToString());  
  Console.ReadKey();
}

Summary  

In this post we covered on how to get system IP address using C# or VB.NET. Hope you found this post helpful. Post your feedback in the comments sections.