예제 #1
0
 public static IPAddress GetLocalIPAddress()
 {
     try
     {
         IPHostEntry host  = Dns.GetHostEntry(Dns.GetHostName());
         IPAddress   local = null;
         foreach (IPAddress ip in host.AddressList)
         {
             if (local is null || ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
             {
                 local = ip;
             }
         }
         if (local is null)
         {
             throw new ApplicationException("Unable to determine local ip address, is the network adapter enabled?");
         }
         return(local.Clean());
     }
     catch
     {
     }
     return(null);
 }