/// <summary>
/// 获取本地连接ip
/// </summary>
/// <returns></returns>
private string getLocalIp()
{
NetworkInterface[] interfaces = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
int len = interfaces.Length;
string localIP = "未找到";
for (int i = 0; i < len; i++)
{
NetworkInterface ni = interfaces[i];
if (ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet || ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
{
if (ni.Name == "本地连接" || ni.Name=="无线网络连接")
{
IPInterfaceProperties property = ni.GetIPProperties();
foreach (UnicastIPAddressInformation ip in property.UnicastAddresses)
{
if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
localIP= ip.Address.ToString();
}
}
}
}
}
return localIP;
}
直接上一段用于内网获取本机ip地址的方法,更全面的可以先获取本机网络类型,在获取ip
最新推荐文章于 2024-05-06 11:43:09 发布