Saturday, 9 April 2011

How To Get IP Address Of A Machine

 Getting Ip address of local machine...

Make sure that you include the System.Net namespace in your code; otherwise the compiler will not know where to look for the DNS class. For more information on the DNS class and System.Net namespace, please refer to the online documentation for the .NET SDK.


<div>
Ip Address : <asp:Label ID="Lbl1" runat="server"></asp:Label>.
</div>

C#

protected void Getip()
    {
        string ipaddress;
        ipaddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        ipaddress = Request.ServerVariables["REMOTE_ADDR"];
        IPAddress[] serveraddress = System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName());
        string localip = Request.UserHostAddress;
        if (localip == "127.0.0.1")
        {
            if (serveraddress.Length == 1)
            {
                ip = serveraddress[0].ToString();
                Lbl1.Text = ip;
            }
            else
            {
                ip = serveraddress[1].ToString();

            }
        }
        
    }


No comments:

Post a Comment