본문 바로가기

IT/c#

[c#] c#에서 내 컴퓨터 IP주소를 받아오는 함수 만들기

private static string getIP()

{

    Regex regex = new Regex(@"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$");


    foreach (System.Net.IPAddress ip in System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList)

    {

        if (regex.IsMatch(ip.ToString()))

        {

            return ip.ToString();

        }

    }

    return null;

}