Find IP address

ddanbe 1 Tallied Votes 319 Views Share

A little console application in C# to show you the IP address of your computer or the IP address of any website you like.

Lusiphur commented: Useful tips from ddanbe as always :twisted: +1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Net;

namespace IPAddressApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            //Get IP info about this computer
            //
            Console.WriteLine("Local host:");
            Console.WriteLine();
            string Name = Dns.GetHostName();
            Console.WriteLine("\tHost Name:          " + Name);
            Line();
            
            DisplayHostInfo(Name);
            Line();

            //Get IP info of an internet address
            //
            DisplayHostInfo("www.amazon.com");
            Line();

            Console.ReadKey();
        }

        static void Line()
        {
            Console.WriteLine("\t============================================");
        }

        static void DisplayHostInfo(string Host)
        {
            IPHostEntry hostStuff;

            hostStuff = Dns.GetHostEntry(Host);

            // Start displaying info
            // DNS name of the host
            Console.WriteLine("\tPrimary host name: " + hostStuff.HostName);

            // Alias names associated with the host
            Console.Write("\tAliases:         ");
            foreach (string alias in hostStuff.Aliases)
            {
                Console.WriteLine("\t\t" + alias);
            }
            Console.WriteLine();

            // List of IP addresses associated with the host
            // May be IPv4 or IPv6 forms
            Console.WriteLine("\tIP Addresses:       ");
            foreach (IPAddress ip in hostStuff.AddressList)
            {
                Console.WriteLine("\t\t" + ip.ToString());
            }
            Console.WriteLine();

        }
    }
}
eswarpabolu 0 Newbie Poster

useful one

eswarpabolu 0 Newbie Poster

really works

chan_lemo 0 Newbie Poster

you can do it in three lines.

IPHostEntry hostname = Dns.GetHostByName("hostname");
IPAddress[] ip = hostname.AddressList;
string str = ip[0].ToString();

http://csharp.net-informations.com/communications/csharp-ip-address.htm

chan

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.