C# 获取本地系统IP地址
时间: 2023-08-28 11:05:31 浏览: 114
C#获取本地IP
你可以使用C#中的`NetworkInterface`类来获取本地系统的IP地址。下面是一个示例代码:
```csharp
using System;
using System.Net;
using System.Net.NetworkInformation;
class Program
{
static void Main()
{
foreach (NetworkInterface networkInterface in NetworkInterface.GetAllNetworkInterfaces())
{
if (networkInterface.OperationalStatus == OperationalStatus.Up)
{
IPInterfaceProperties ipProps = networkInterface.GetIPProperties();
foreach (UnicastIPAddressInformation address in ipProps.UnicastAddresses)
{
if (address.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
Console.WriteLine("IP Address: " + address.Address);
}
}
}
}
}
}
```
运行这段代码将会输出本地系统的所有IPv4地址。
阅读全文