How the things will be? |
---|
1.Get the host name first by Dns.GetHostName(); |
2. Save the IP address in IPHostEntry by Dns.GetHostByName(hostName); |
3. Using IPAddress array, get the actual host address. |
4. Using IPAddress array, get the host address of local. |
5. Compare the results of step 3 and 4. |
- using System;
- using System.Net;
- public class ForgetCode
- {
- static string IPHost;
- public static void Main()
- {
- UseDNS();
- }
- public static void UseDNS()
- {
- string hostName = Dns.GetHostName();
- IPHostEntry local = Dns.GetHostByName(hostName);
- foreach (IPAddress ipaddress in local.AddressList)
- {
- IPHost = ipaddress.ToString();
- }
- if (isLocal(IPHost))
- {
- Console.WriteLine("Yes, it is a local system");
- }
- else
- {
- Console.WriteLine("No, it is not a local system");
- }
- }
- public static bool isLocal(string host)
- {
- try
- {
- IPAddress[] hostIPs = Dns.GetHostAddresses(host);
- // get local IP addresses
- IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
- // test if any host IP equals to any local IP or to localhost
- foreach (IPAddress hostIP in hostIPs)
- {
- // is localhost
- if (IPAddress.IsLoopback(hostIP)) return true;
- // is local address
- foreach (IPAddress localIP in localIPs)
- {
- if (hostIP.Equals(localIP)) return true;
- }
- }
- }
- catch { }
- return false;
- }
- }
Yes, it is a local system |
---|