Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have to catch and store my local system IP using C# ?

how could i do ?
Posted

Did you try to use Google[^]?

You can make use of GetHostAddresses [^]

Something like this would be good to start with:

C#
IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
foreach (IPAddress addr in localIPs)
{
    Console.WriteLine(addr);
}
 
Share this answer
 
Comments
Naveen Singh 24-Sep-14 2:33am    
Thanks Manas But it shows 'localIPs[0].Address' threw an exception of type 'System.Net.Sockets.SocketException'

and

returns some what like ::1
Sergey Alexandrovich Kryukov 24-Sep-14 2:41am    
Well, DNS might be not properly configured.
Did you first check up the number of addresses? It could be 0...
—SA
Bernhard Hiller 24-Sep-14 9:44am    
Do you understand that ::1 is the IPv6 equivalent of 127.0.0.1?
Sergey Alexandrovich Kryukov 24-Sep-14 2:41am    
5ed. Important: the IP address is not just one.
—SA
Naveen Singh 24-Sep-14 2:43am    
hi SA...! yes Ip contains one valid address and one address like ::1 but i have to fetch actual address how could i get that ?
C#
{
            IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
            foreach (IPAddress addr in localIPs)
                             
            {
              //  Console.WriteLine(addr);
                Console.Write(localIPs[1]);
            }

        }



Here the solution if configuration have more than one IP
 
Share this answer
 
Comments
Bernhard Hiller 24-Sep-14 9:45am    
So you have it write several times the second address of the available addresses? Why several times?
you can use following code:-

IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
 
Share this answer
 
from this you can get local ip address:
C#
namespace NKUtilities
{
    using System;
    using System.Net;

    public class DNSUtility
    {
        public static int Main (string [] args)
        {

          String strHostName = new String ("");
          if (args.Length == 0)
          {
              // Getting Ip address o<code></code>f local machine...
              // First get the host name of local machine.
              strHostName = DNS.GetHostName ();
              Console.WriteLine ("Local Machine's Host Name: " +  strHostName);
          }
          else
          {
              strHostName = args[0];
          }

          // Then using host name, get the IP address list..
          IPHostEntry ipEntry = DNS.GetHostByName (strHostName);
          IPAddress [] addr = ipEntry.AddressList;

          for (int i = 0; i < addr.Length; i++)
          {
              Console.WriteLine ("IP Address {0}: {1} ", i, addr[i].ToString ());
          }
          return 0;
        }
     }
}

from this you will get client ip address:
C#
string VisitorsIPAddr = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                   if (VisitorsIPAddr != null || VisitorsIPAddr !=String.Empty)
                   {
                       VisitorsIPAddr = Request.ServerVariables["REMOTE_ADDR"];
                   }
                   else if (HttpContext.Current.Request.UserHostAddress.Length != 0)
                   {
                       VisitorsIPAddr = HttpContext.Current.Request.UserHostAddress;
                   }
                   string str=VisitorsIPAddr;


"VisitorsIPAddr" from this variable you can get client system ip address
 
Share this answer
 
following code may help you..


C#
static void Main(string[] args)
       {
           string sHostName = Dns.GetHostName();
           Console.WriteLine("Name of the System:" + sHostName);
           IPHostEntry ipE =Dns.GetHostByName(sHostName);
           IPAddress[] IpA = ipE.AddressList;
           for (int i = 0; i < IpA.Length; i++)
           {
               Console.WriteLine("IP Address {0}: {1} ", i, IpA[i].ToString());
           }

           Console.ReadLine();
       }


thanks!
 
Share this answer
 
Check Local IP Address [C#]
This example shows how to detect whether a host name or IP address belongs to local computer.
Get local computer name
Get local computer host name using static method Dns.GetHostName.
[C#]
string localComputerName = Dns.GetHostName();


Get local IP address list
Get list of computer IP addresses using static method Dns.GetHostAd¬dresses. To get list of local IP addresses pass local computer name as a parameter to the method.
[C#]
IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());


Check whether an IP address is local
The following method checks if a given host name or IP address is local. First, it gets all IP addresses of the given host, then it gets all IP addresses of the local computer and finally it compares both lists. If any host IP equals to any of local IPs, the host is a local IP. It also checks whether the host is a loopback address (localhost / 127.0.0.1).
[C#]
public static bool IsLocalIpAddress(string host)
{
  try
  { // get host IP addresses
    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;
}


You can test the method for example like this:
[C#]
IsLocalIpAddress("localhost");        // true (loopback name)
IsLocalIpAddress("127.0.0.1");        // true (loopback IP)
IsLocalIpAddress("MyNotebook");       // true (my computer name)
IsLocalIpAddress("192.168.0.1");      // true (my IP)
IsLocalIpAddress("NonExistingName");  // false (non existing computer name)
IsLocalIpAddress("99.0.0.1");         // false (non existing IP in my net)


private string ReturnIPAddress() 
        {
            IPHostEntry host;
            string localIP = "?";
            host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    localIP = ip.ToString();
                }
            }
            return localIP;
        }
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900