Test for local host

Posted on 2013-05-31
Last Modified: 2013-06-01
How can I detect if the local host is running?


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

Question by:Dovberman
  • 2
LVL 12

Expert Comment

by:jitendra patil
ID: 39212493
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(""));

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


        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.
LVL 16

Accepted Solution

Rose Babu earned 300 total points
ID: 39212545
Try this...
HttpContext context = HttpContext.Current;

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

	// running from Remote Host


Open in new window


Author Comment

ID: 39212788
I used the suggestion from srosebabu.

Thanks to both of you.

Author Closing Comment

ID: 39212790
Thank you

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Get distinct values from excel or comm seperated file 4 30
Convert Select to DropDownListFor MVC 5 2 30
Easy filter aspnet 2 24
Google Maps with Webforms 1 12
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question