.NET getIP

  • 68
  • 0

.NET getIP

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Sockets;

namespace Pratice_Extjs
{
    public partial class getIP : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            string ip = GetLocalIPAddress();
            Response.Write(ip);
        }

        public static string GetLocalIPAddress()
        {
            var host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (var ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    return ip.ToString();
                }
            }
            throw new Exception("No network adapters with an IPv4 address in the system!");
        }

    }
}

資料來源:

Stackoverflow