using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Net.NetworkInformation;
using System.Net.Sockets;
public class CmdTcp
{
string serverIP = String.Empty;
int serverPort = 6000;
private void GetLocalIP()
{
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); //获取说有网卡信息
foreach (NetworkInterface adapter in nics)
{
//Wireless80211--无线网卡 Ppp--宽带连接 Ethernet--以太网卡
if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet && (adapter.Name == "以太网" || adapter.Name == "本地连接")) //判断是否为以太网卡
{
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
serverIP = ipadd.Address.ToString();//获取ip
}
}
}
}
}
C# 获取本地IP
最新推荐文章于 2023-12-20 21:20:48 发布