在C#开发中,获取本机IP地址是网络编程的基础操作,不同场景下需要选择不同的实现方式。下面先展示一张相关示意图,帮助理解网络接口和IP地址的关系。

方法一:通过NetworkInterface获取所有活跃网络接口的IPv4地址
这种方式可以过滤掉非活跃的网络接口,只获取当前正在使用的IPv4地址,适配大多数常规场景。
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.NetworkInformation;
public class IPHelper
{
// 获取本机所有活跃IPv4地址
public static List<string> GetActiveIPv4ByNetworkInterface()
{
List<string> ipList = new List<string>();
// 获取所有网络接口
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface ni in interfaces)
{
// 过滤非活跃、回环、隧道类型的接口
if (ni.OperationalStatus != OperationalStatus.Up) continue;
if (ni.NetworkInterfaceType == NetworkInterfaceType.Loopback) continue;
if (ni.NetworkInterfaceType == NetworkInterfaceType.Tunnel) continue;
// 获取接口的IP属性
IPInterfaceProperties ipProps = ni.GetIPProperties();
foreach (UnicastIPAddressInformation ipInfo in ipProps.UnicastAddresses)
{
// 只保留IPv4地址,排除回环地址
if (ipInfo.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
if (!IPAddress.IsLoopback(ipInfo.Address))
{
ipList.Add(ipInfo.Address.ToString());
}
}
}
}
return ipList;
}
}方法二:通过Dns获取本机主机名对应的IP地址
这种方式实现简单,但是可能会返回IPv6地址,需要额外过滤。
using System;
using System.Collections.Generic;
using System.Net;
public class IPHelper
{
// 通过Dns获取本机IPv4地址
public static List<string> GetIPv4ByDns()
{
List<string> ipList = new List<string>();
// 获取本机主机名
string hostName = Dns.GetHostName();
// 根据主机名获取所有IP地址
IPAddress[] addresses = Dns.GetHostAddresses(hostName);
foreach (IPAddress addr in addresses)
{
// 只保留IPv4地址,排除回环地址
if (addr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
if (!IPAddress.IsLoopback(addr))
{
ipList.Add(addr.ToString());
}
}
}
return ipList;
}
}方法三:获取指定网络接口的IP地址
如果需要获取特定类型网络接口的IP,比如以太网、无线网,可以使用这种方式。
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.NetworkInformation;
public class IPHelper
{
// 获取指定类型网络接口的IPv4地址
public static List<string> GetIPv4ByInterfaceType(NetworkInterfaceType targetType)
{
List<string> ipList = new List<string>();
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface ni in interfaces)
{
// 匹配指定类型的接口,且接口处于活跃状态
if (ni.NetworkInterfaceType != targetType) continue;
if (ni.OperationalStatus != OperationalStatus.Up) continue;
IPInterfaceProperties ipProps = ni.GetIPProperties();
foreach (UnicastIPAddressInformation ipInfo in ipProps.UnicastAddresses)
{
if (ipInfo.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
ipList.Add(ipInfo.Address.ToString());
}
}
}
return ipList;
}
}
// 调用示例:获取以太网接口的IP
// var ethernetIPs = GetIPv4ByInterfaceType(NetworkInterfaceType.Ethernet);方法四:获取本机所有IP地址(包含IPv6)
如果需要同时获取IPv4和IPv6地址,可以使用这个通用方法。
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.NetworkInformation;
public class IPHelper
{
// 获取本机所有活跃的IP地址(包含IPv4和IPv6)
public static List<string> GetAllActiveIPs()
{
List<string> ipList = new List<string>();
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface ni in interfaces)
{
if (ni.OperationalStatus != OperationalStatus.Up) continue;
if (ni.NetworkInterfaceType == NetworkInterfaceType.Loopback) continue;
IPInterfaceProperties ipProps = ni.GetIPProperties();
foreach (UnicastIPAddressInformation ipInfo in ipProps.UnicastAddresses)
{
ipList.Add(ipInfo.Address.ToString());
}
}
return ipList;
}
}方法五:获取本机默认网关对应的网络接口IP
这种方式可以获取到当前用于访问外网的网络接口IP,适合需要对外通信的场景。
using System;
using System.Net;
using System.Net.NetworkInformation;
public class IPHelper
{
// 获取默认网关对应的网络接口IPv4地址
public static string GetDefaultGatewayIPv4()
{
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface ni in interfaces)
{
if (ni.OperationalStatus != OperationalStatus.Up) continue;
// 获取网关地址
GatewayIPAddressInformationCollection gateways = ni.GetIPProperties().GatewayAddresses;
if (gateways.Count > 0)
{
// 找到有网关的接口,返回其第一个IPv4地址
foreach (UnicastIPAddressInformation ipInfo in ni.GetIPProperties().UnicastAddresses)
{
if (ipInfo.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
return ipInfo.Address.ToString();
}
}
}
}
return string.Empty;
}
}方法六:.NET Core/.NET 5+ 使用System.Net.NetworkInformation扩展方法
高版本.NET框架中可以使用更简洁的扩展方法实现,代码可读性更高。
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.NetworkInformation;
using System.Linq;
public class IPHelper
{
// .NET Core及以上版本获取活跃IPv4地址
public static List<string> GetIPv4ForDotNetCore()
{
return NetworkInterface.GetAllNetworkInterfaces()
.Where(ni => ni.OperationalStatus == OperationalStatus.Up)
.Where(ni => ni.NetworkInterfaceType != NetworkInterfaceType.Loopback)
.SelectMany(ni => ni.GetIPProperties().UnicastAddresses)
.Where(ip => ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
.Select(ip => ip.Address.ToString())
.ToList();
}
}方法七:通过Socket连接获取本机对外IP
这种方式可以获取到本机在局域网中对外通信使用的IP,适合需要和其他设备通信的场景。
using System;
using System.Net;
using System.Net.Sockets;
public class IPHelper
{
// 通过Socket连接获取本机对外IPv4地址
public static string GetExternalIPv4BySocket()
{
try
{
// 创建一个UDP Socket,连接到公网地址(不需要真正建立连接)
using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
{
// 连接到任意公网IP和端口,这里使用8.8.8.8:80
socket.Connect("8.8.8.8", 80);
// 获取本地端点地址
IPEndPoint endPoint = socket.LocalEndPoint as IPEndPoint;
if (endPoint != null)
{
return endPoint.Address.ToString();
}
}
}
catch
{
// 连接失败返回空
}
return string.Empty;
}
}方法对比与选择建议
不同方法的适用场景不同,开发者可以根据需求选择:
| 方法 | 适用场景 | 优点 | 缺点 |
|---|---|---|---|
| 方法一 | 常规获取本机活跃IPv4地址 | 过滤准确,适配大多数场景 | 代码稍长 |
| 方法二 | 快速获取IP,不关心IPv6 | 实现简单 | 可能返回IPv6,需要额外过滤 |
| 方法三 | 需要指定网络接口类型 | 精准获取特定接口IP | 需要明确接口类型 |
| 方法四 | 需要同时获取IPv4和IPv6 | 覆盖所有地址类型 | 包含不需要的地址类型 |
| 方法五 | 需要获取访问外网的接口IP | 对应实际对外通信的IP | 依赖网关存在 |
| 方法六 | .NET Core及以上版本项目 | 代码简洁,可读性强 | 不支持低版本.NET Framework |
| 方法七 | 获取局域网对外通信IP | 对应实际通信使用的IP | 需要网络连接,可能失败 |
实际开发中,推荐优先使用方法一或者方法六,根据项目的.NET版本选择即可,这两种方式覆盖的场景最全面,稳定性也更高。
C#本机IP获取NetworkInterfaceDnsIPAddress修改时间:2026-05-29 04:30:24