본문으로 바로가기

IPAddress 가져오기

category Development/C# 2012. 12. 21. 14:50

XP의 경우 로컬IP를 받아올땐 아래와 같이

IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
string ipAddr = hostEntry.AddressList[0].ToString();

하지만, 위와 같은 경우 비스타 또는 윈도우7은 ipv6값으로 받아오게 된다.

그러므로 IP값을 ipv4로 받아오기 위해서는 아래와 같은 방법으로 해야한다.

    public class Host
    {
        public static string IPAddress
        {
            get
            {
                string ipaddr = string.Empty;

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

                foreach (var i in hostEntry.AddressList)
                {
                    if (i.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        return i.ToString();
                }

                return string.Empty;
            }
        }
    } 


이렇게 하면 Host.IPAddress로 로컬아이피를 받아올수 있다.

'Development > C#' 카테고리의 다른 글

using 과 Dispose  (0) 2013.01.04
제네릭(Generic)과 제약조건  (0) 2012.12.28
List 매서드 재정의  (0) 2012.12.13
최상위 컨트롤  (0) 2012.12.13
ClickOnce 삭제  (0) 2012.12.13