Get local IP address in C#
To get local IP Address in C#, first we need to get host object usingDns.GetHostEntry(Dns.GetHostName())
, host object will have a list of addresses for each interface available in the local machine, we can then loop through these addresses and get all IPs.Note: You will need to use
using System.Net;
Example
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}