Solved

Test for local host

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

example:

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

Thanks
0
Comment
Question by:Dovberman
  • 2
4 Comments
 
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("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
 
LVL 16

Accepted Solution

by:
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

}
else
{
	// running from Remote Host

}

Open in new window

0
 

Author Comment

by:Dovberman
ID: 39212788
I used the suggestion from srosebabu.

Thanks to both of you.
0
 

Author Closing Comment

by:Dovberman
ID: 39212790
Thank you
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

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 …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

895 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now