Test for local host

How can I detect if the local host is running?

example:

if (local host)
     {
      Do something
     }
else  //remote host
     {
      Do Nothing
     }

Thanks
DovbermanAsked:
Who is Participating?
 
Rose BabuConnect With a Mentor Senior Team ManagerCommented:
Try this...
HttpContext context = HttpContext.Current;

if (context.Request.Url.ToString().StartsWith("http://localhost"))
{
	// running from Local Host

}
else
{
	// running from Remote Host

}

Open in new window

0
 
Jitendra PatilSr.Software EngineerCommented:
try the code given below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.NetworkInformation;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
                Console.WriteLine("Local IP Address: " + GetIPAddress(Dns.GetHostName()));
                Console.WriteLine("Google IP:" + GetIPAddress("google.com"));

                if (GetIPAddress(Dns.GetHostName()).ToString() == "::1")
                {
                    Console.WriteLine("This is local host address");
                }
                else
                {
                    Console.WriteLine("This is website address");
                }

                Console.ReadLine();
        }

        public static IPAddress GetIPAddress(string hostName)
        {
            Ping ping = new Ping();
            var replay = ping.Send(hostName);

            if (replay.Status == IPStatus.Success)
            {
                return replay.Address;
            }
            return null;
        }

    }
}

Open in new window


hope this helps.
0
 
DovbermanAuthor Commented:
I used the suggestion from srosebabu.

Thanks to both of you.
0
 
DovbermanAuthor Commented:
Thank you
0
All Courses

From novice to tech pro — start learning today.