Namespace: System.Net 추가

           string strHostName = Dns.GetHostName();

           IPHostEntry ipEntry = Dns.GetHostByName(strHostName);
           IPAddress[] addr = ipEntry.AddressList;

           for (int i = 0; i < addr.Length; i++)
           {
               Console.WriteLine("IP Address {0}: {1} ", i, addr[i].ToString());
           }

 

아래는 MSDN

The following example uses the GetHostByName method to get the DNS information for the specified DNS host name.

      try 
      {
         IPHostEntry hostInfo = Dns.GetHostByName(hostName);
         // Get the IP address list that resolves to the host names contained in the 
         // Alias property.
         IPAddress[] address = hostInfo.AddressList;
         // Get the alias names of the addresses in the IP address list.
         String[] alias = hostInfo.Aliases;

         Console.WriteLine("Host name : " + hostInfo.HostName);
         Console.WriteLine("\nAliases : ");
         for(int index=0; index < alias.Length; index++) {
           Console.WriteLine(alias[index]);
         } 
         Console.WriteLine("\nIP address list : ");
         for(int index=0; index < address.Length; index++) {
            Console.WriteLine(address[index]);
         }
      }
      catch(SocketException e) 
      {
         Console.WriteLine("SocketException caught!!!");
         Console.WriteLine("Source : " + e.Source);
         Console.WriteLine("Message : " + e.Message);
      }
      catch(ArgumentNullException e)
      {
     Console.WriteLine("ArgumentNullException caught!!!");
         Console.WriteLine("Source : " + e.Source);
         Console.WriteLine("Message : " + e.Message);
      }
      catch(Exception e)
      {
          Console.WriteLine("Exception caught!!!");
          Console.WriteLine("Source : " + e.Source);
          Console.WriteLine("Message : " + e.Message);
      }

+ Recent posts