C#获取当前不同网卡对应的iP

C#获取当前不同网卡对应的iP:

public string GetLocalIP()
    {
        IPAddress localIp = null;
        try
        {
            IPAddress[] ipArray;
            ipArray = Dns.GetHostAddresses(Dns.GetHostName());
            localIp = ipArray.First(ip => ip.AddressFamily == AddressFamily.InterNetwork);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.StackTrace + "\r\n" + ex.Message, "错误", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
            Log.WriteLog(ex);
        }
        if (localIp == null)
        {
            localIp = IPAddress.Parse("127.0.0.1");
        }
        return localIp.ToString();
    }
ipArray = Dns.GetHostAddresses(Dns.GetHostName());会返回一个IP数组,里面会有多个IP地址
localIp = ipArray.First(ip => ip.AddressFamily == AddressFamily.InterNetwork);会返回符合IP格式的第一个数据,想获取多个可以往后获取。

你可能感兴趣的:(C#获取当前不同网卡对应的iP)