Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#获取活动NIC IPv4地址_C#_Ip Address - Fatal编程技术网

C#获取活动NIC IPv4地址

C#获取活动NIC IPv4地址,c#,ip-address,C#,Ip Address,我的电脑上有多张网卡。(因为VMWare) 如何找到活动卡的IPv4地址。我的意思是,如果我在终端中发送ping并在WireShark中截获数据包,我需要“源”的地址 我想检查每个网络接口,看看网关是空的还是空的?或者ping 127.0.0.1并获取ping请求的IP源?但却无法实施 现在,我有我在StackOverFlow上找到的代码 public static string GetLocalIpAddress() { var host = Dns.

我的电脑上有多张网卡。(因为VMWare)

如何找到活动卡的IPv4地址。我的意思是,如果我在终端中发送ping并在WireShark中截获数据包,我需要“源”的地址

我想检查每个网络接口,看看网关是空的还是空的?或者ping 127.0.0.1并获取ping请求的IP源?但却无法实施

现在,我有我在StackOverFlow上找到的代码

 public static string GetLocalIpAddress()
        {
            var host = Dns.GetHostEntry(Dns.GetHostName());
            return host.AddressList.First(h => h.AddressFamily == AddressFamily.InterNetwork).ToString();
        }

但它为我提供了VmWare卡的IP。但是我不知道还有什么“
.First()
”我可以用。

好吧,我的朋友,你可以做以下几件事:

var nics = NetworkInterface.GetAllNetworkInterfaces();
foreach (var networkInterface in nics)
{
    if (networkInterface.OperationalStatus == OperationalStatus.Up)
    {
        var address = networkInterface.GetPhysicalAddress();
    }
}

address变量允许您访问当前正在运行的网络接口的物理地址。好了,我的朋友,您可以执行以下操作:

var nics = NetworkInterface.GetAllNetworkInterfaces();
foreach (var networkInterface in nics)
{
    if (networkInterface.OperationalStatus == OperationalStatus.Up)
    {
        var address = networkInterface.GetPhysicalAddress();
    }
}

address变量允许您访问当前正在运行的网络接口的物理地址

我终于找到了一种获取真实IP的有效方法。基本上,它会查找IPv4中的所有接口,这些接口都已启动,而让它决定的是,它只接受带有默认网关的接口

public static string GetLocalIpAddress() 
        {
            foreach (var netI in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (netI.NetworkInterfaceType != NetworkInterfaceType.Wireless80211 &&
                    (netI.NetworkInterfaceType != NetworkInterfaceType.Ethernet ||
                     netI.OperationalStatus != OperationalStatus.Up)) continue;
                foreach (var uniIpAddrInfo in netI.GetIPProperties().UnicastAddresses.Where(x => netI.GetIPProperties().GatewayAddresses.Count > 0))
                {

                    if (uniIpAddrInfo.Address.AddressFamily == AddressFamily.InterNetwork &&
                        uniIpAddrInfo.AddressPreferredLifetime != uint.MaxValue)
                        return uniIpAddrInfo.Address.ToString();
                }
            }
            Logger.Log("You local IPv4 address couldn't be found...");
            return null;
        }

我终于找到了一种获得真正IP的有效方法。基本上,它会查找IPv4中的所有接口,这些接口都已启动,而让它决定的是,它只接受带有默认网关的接口

public static string GetLocalIpAddress() 
        {
            foreach (var netI in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (netI.NetworkInterfaceType != NetworkInterfaceType.Wireless80211 &&
                    (netI.NetworkInterfaceType != NetworkInterfaceType.Ethernet ||
                     netI.OperationalStatus != OperationalStatus.Up)) continue;
                foreach (var uniIpAddrInfo in netI.GetIPProperties().UnicastAddresses.Where(x => netI.GetIPProperties().GatewayAddresses.Count > 0))
                {

                    if (uniIpAddrInfo.Address.AddressFamily == AddressFamily.InterNetwork &&
                        uniIpAddrInfo.AddressPreferredLifetime != uint.MaxValue)
                        return uniIpAddrInfo.Address.ToString();
                }
            }
            Logger.Log("You local IPv4 address couldn't be found...");
            return null;
        }

谢谢你的回答。这种类型的实现的问题在于,它将向您提供所有向上的inytefaces。我的其他牌也都挂了。我已经发布了我最终提出的解决方案是的,但是您在问题的代码示例中选择了第一个。我的解决方案向您展示了所有接口,您可以选择其中一个。顺便说一句,你找到了答案,这才是最重要的。谢谢你的回答。这种类型的实现的问题在于,它将向您提供所有向上的inytefaces。我的其他牌也都挂了。我已经发布了我最终提出的解决方案是的,但是您在问题的代码示例中选择了第一个。我的解决方案向您展示了所有接口,您可以选择其中一个。顺便说一句,你找到了答案,这才是最重要的。谢谢你的更新,我刚刚尝试了你的方法。在窗户下似乎很好用。但是,请注意Linux下的.NET Core(2.1)(通过Docker在Linux容器上测试):这里不支持AddressPreferredLifetime属性,因此如果您想要跨平台代码,最好不要使用它!但是我有两张默认网关设置的网卡谢谢你的更新,我刚刚试过你的方法。在窗户下似乎很好用。但是,请注意Linux下的.NET Core(2.1)(通过Docker在Linux容器上测试):这里不支持AddressPreferredLifetime属性,因此如果您想要跨平台代码,最好不要使用它!但我有两张网卡,设置了默认网关