How to get the IP Address of the Local Machine using C# ?
You can get the IP address of the local machine or localhost using C# using the Dns class’s GetHostByName defined in the System.Net namespace.
The Dns.GetHostByName returns the array of AddressList which contains the IP address .
How to get the IP Address of the Local Machine using C# ?
Below is the sample code snippet demonstrating the retrieval of the IP address of the local machine or localhost in C#.
C#
x
22
22
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Net;
5
6
namespace AbundantCodeConsoleApp
7
{
8
internal class Program
9
{
10
private static void Main(string[] args)
11
{
12
// Get the Name of the Host
13
string host = Dns.GetHostName();
14
15
// Get the IP address
16
string ip = Dns.GetHostByName(host).AddressList[0].ToString();
17
// Display the IP
18
Console.WriteLine("Local Host IP is " + ip);
19
Console.ReadLine();
20
}
21
}
22
}

Leave Your Comment