一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

asp.net中c#获取本机IP地址实例代码

时间:2013-05-28 编辑:简简单单 来源:一聚教程网

例1

 代码如下 复制代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace CSharpTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write(new Program().GetHostInfo());
            Console.ReadLine();
        }
        //获取本地IPd等信息
        protected string GetHostInfo()  
        {
            StringBuilder hostInfo = new StringBuilder("");
            IPAddress[] ipHost = Dns.GetHostAddresses(Dns.GetHostName());
            hostInfo.Append("本机名:");
            hostInfo.Append(Dns.GetHostName());
            hostInfo.Append("rn");
            hostInfo.Append("IP 地址:");
            hostInfo.Append("rn");

            foreach (IPAddress address in ipHost)
            {
                hostInfo.Append(address.ToString());
                hostInfo.Append("rn");
            }

            return hostInfo.ToString();
        }
    }
}


 

控制台输出结果:

 本机名:meteor-PC
IP 地址:
fe80::a1a2:949c:1a51:5e6c%11
2002:b46f:20fa::b46f:20fa
169.254.94.108
180.111.32.250

例2

 代码如下 复制代码


public static IPAddress GetHostIP()
{
IPHostEntry ipe = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ip = ipe.AddressList[0];


return ip;
}
private String GetIPAddress()
{
String str;
String Result = "";
String hostName = Dns.GetHostName();
IPAddress[] myIP = Dns.GetHostAddresses(hostName);
foreach (IPAddress address in myIP)
{
str = address.ToString();
for (int i = 0; i < str.Length; i++)
{
if (str[i] >= '0' && str[i] <= '9' || str[i] == '.') Result = str;
}
}
return Result;
}

例3

上面的方法满足不了我的需求,然后我在Google搜了一些,并没有个很好的办法,于是自己摸索写下了这个方法。如果觉得有些地方欠妥的

 代码如下 复制代码

///


/// 得到本机IP
///

private string GetLocalIP() 
{
    //本机IP地址
    string strLocalIP = "";
    //得到计算机名
    string strPcName = Dns.GetHostName();
    //得到本机IP地址数组
    IPHostEntry ipEntry = Dns.GetHostEntry(strPcName);
    //遍历数组
    foreach(var IPadd in ipEntry.AddressList)
    {
        //判断当前字符串是否为正确IP地址
        if (IsRightIP(IPadd.ToString())) 
        {
            //得到本地IP地址
            strLocalIP = IPadd.ToString();
            //结束循环
            break;
        }
    }
 
    //返回本地IP地址
    return strLocalIP;
}
  

?//得到网关地址
private string GetGateway() 

    //网关地址
    string strGateway = "";
    //获取所有网卡
    NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
    //遍历数组
    foreach (var netWork in nics)
    {
        //单个网卡的IP对象
        IPInterfaceProperties ip = netWork.GetIPProperties();
        //获取该IP对象的网关
        GatewayIPAddressInformationCollection gateways = ip.GatewayAddresses;
        foreach(var gateWay in gateways)
        {
            //如果能够Ping通网关
            if(IsPingIP(gateWay.Address.ToString()))
            {
                //得到网关地址
                strGateway = gateWay.Address.ToString();
                //跳出循环
                break;
            }
        }
 
        //如果已经得到网关地址
        if (strGateway.Length > 0) 
        {
            //跳出循环
            break;
        }
    }
 
    //返回网关地址
    return strGateway;
}
  

?///


/// 判断是否为正确的IP地址
///

/// 需要判断的字符串
/// true = 是 false = 否
public static bool IsRightIP(string strIPadd) 
{
    //利用正则表达式判断字符串是否符合IPv4格式
    if (Regex.IsMatch(strIPadd, "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"))
    {
        //根据小数点分拆字符串
        string[] ips = strIPadd.Split('.');
        if (ips.Length == 4 || ips.Length == 6)
        {
            //如果符合IPv4规则
            if (System.Int32.Parse(ips[0]) < 256 && System.Int32.Parse(ips[1]) < 256 & System.Int32.Parse(ips[2]) < 256 & System.Int32.Parse(ips[3]) < 256)
                //正确
                return true;
            //如果不符合
            else
                //错误
                return false;
        }
        else
            //错误
            return false;
    }
    else
        //错误
        return false;
}
  

?///


/// 尝试Ping指定IP是否能够Ping通
///

/// 指定IP
/// true 是 false 否
public static bool IsPingIP(string strIP) 
{
    try
    {
        //创建Ping对象
        Ping ping = new Ping();
        //接受Ping返回值
        PingReply reply = ping.Send(strIP, 1000);
        //Ping通
        return true;
    }
    catch
    {
        //Ping失败
        return false;
    }
}

热门栏目