How to Get the IP address in Winforms/Console Application using C#?

Below is a sample code snippet demonstrating in simple steps on how to retrieve the IP address in the Winforms or Console APPLICATION using c#.

How to Get the IP address in Winforms/Console Application using C#?

using System;

using System.Collections.Generic;

using System.Data;

using System.DirectoryServices.AccountManagement;

using System.IO;

using System.Linq;

using System.Net;

using System.Net.Sockets;

namespace AbundantCode

{

internal class Program

{

// How to Validate a user in Active Directory using C# ?

private static void Main(string[] args)

{

IPHostEntry SystemAC = Dns.GetHostEntry(Dns.GetHostName());

string IPAddress = string.Empty;

foreach (var address in SystemAC.AddressList)

{

if (address.AddressFamily == AddressFamily.InterNetwork)

{

IPAddress = address.ToString();

}

}

Console.WriteLine(IPAddress);

Console.ReadKey();

}

}

}
%d