【问题标题】:Get specific system ip address using c#使用c#获取特定的系统ip地址
【发布时间】:2022-12-09 23:53:32
【问题描述】:

我使用这种类型的代码来获取我的系统 IPv4 地址:

string ipadress = Dns.GetHostEntry(Dns.GetHostName())
.AddressList.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork)
.ToString();
System.Windows.Forms.MessageBox.Show(ipadress);

但由于我有不止 1 个网络接口,结果可能不是我需要的。 可以搜索IP以“10.”开头的接口吗? 那就是我需要的。只是网络 10 中接口的 IP 地址....

:)

【问题讨论】:

  • AddressList给你什么数据?

标签: c#


【解决方案1】:

您可以使用 Linq 查询来查找以特定模式开头的 IP。

var ipAddress = Dns.GetHostEntry(Dns.GetHostName())
                .AddressList
                .Where(addr => addr.Address.ToString().StartsWith("10"))
                .ToString();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多