public string GetLocalIP() { //获取说有网卡信息 NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adapter in nics) { //判断是否为以太网卡 //Wireless80211 无线网卡 Ppp 宽带连接 //Ethernet 以太网卡 //这里篇幅有限贴几个常用的,其他的返回值大家就自己百度吧! if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet) { //获取以太网卡网络接口信息 IPInterfaceProperties ip = adapter.GetIPProperties(); //获取单播地址集 UnicastIPAddressInformationCollection ipCollection = ip.UnicastAddresses; foreach (UnicastIPAddressInformation ipadd in ipCollection) { //InterNetwork IPV4地址 InterNetworkV6 IPV6地址 //Max MAX 位址 if (ipadd.Address.AddressFamily == AddressFamily.InterNetwork) //判断是否为ipv4 { strLocalIP = ipadd.Address.ToString();//获取ip return strLocalIP;//获取ip } } } } return null; }
引用:http://bbs.csdn.net/topics/391928616
-