Tuesday, December 3, 2013

How To Get Your System IP Address Using C#.

Create a page namely IP.aspx
Demo :

Copy and Paste the Code in IP.aspx page :


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="css.aspx.cs" Inherits="css" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Get  IP Address</title>
 
<style type="text/css">

.button_example{
border:1px solid #ff6262; -webkit-border-radius: 3px;
 -moz-border-radius: 3px;border-radius: 3px;font-size:12px;font-family:arial, helvetica, sans-serif;
  padding: 10px 10px 10px 10px; text-decoration:none; display:inline-block;text-shadow: -1px -1px 0 rgba(0,0,0,0.3);
  font-weight:bold; color: #FFFFFF;
 background-color: #ff9a9a; background-image: linear-gradient(to bottom, #ff9a9a, #ff4040);
 }

.button_example:hover{
 border:1px solid #ff3434;
 background-color: #ff6767; background-image: linear-gradient(to bottom, #ff6767, #ff0d0d);
 }
</style>
 
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <br />
        <br />
        <pre class="code"
            style="background-color: white; border: 6px solid rgb(234, 234, 234); color: #444444; font-family: 'courier new'; font-size: 13px; line-height: 18px; outline: rgb(212, 212, 212) solid 1px; overflow: auto; padding: 15px; position: relative; text-align: justify; width: 617px; top: 0px; left: 0px; height: 149px;">          WelCome to <a
            href="http://sarojasp.blogspot.in/">http://sarojasp.blogspot.in/</a>  

         IP Address : <asp:Label ID="Lblip" runat="server"></asp:Label>
         
                      <asp:Button ID="Button1" runat="server" Text="Get Your IP"
            OnClick="Button1_Click" CssClass="button_example" Height="36px" />
        </pre>
        <span id = "ipaddress"></span>
    </div>
    </form>
</body>
</html>

After that ,Copy and Paste the Code in IP.aspx.cs Page :

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.NetworkInformation;

public partial class css : System.Web.UI.Page
{
      protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
        {
            if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
            {
                Console.WriteLine(ni.Name);
                foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses)
                {
                    if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                    {
                       Lblip.Text="Your IP address is : "+ip.Address.ToString();
                    }
                }
            }
        }
    }

}




2 comments: