Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.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# 如何以编程方式获取计算机的本地网络IP地址?_C#_.net_.net 3.5_Ip Address - Fatal编程技术网

C# 如何以编程方式获取计算机的本地网络IP地址?

C# 如何以编程方式获取计算机的本地网络IP地址?,c#,.net,.net-3.5,ip-address,C#,.net,.net 3.5,Ip Address,我需要使用C#和.NET 3.5从我的程序中获取计算机的实际本地网络IP地址(例如192.168.0.220)。在这种情况下,我不能只使用127.0.0.1 如何实现这一点?在John Spano的文章中,它说要添加System.Net名称空间,并使用以下代码: //获取本地IP地址 字符串sHostName=Dns.GetHostName(); IPHostEntry ipE=Dns.GetHostByName(sHostName); IPAddress[]IpA=ipE.AddressLis

我需要使用C#和.NET 3.5从我的程序中获取计算机的实际本地网络IP地址(例如192.168.0.220)。在这种情况下,我不能只使用127.0.0.1

如何实现这一点?

在John Spano的文章中,它说要添加
System.Net
名称空间,并使用以下代码:

//获取本地IP地址
字符串sHostName=Dns.GetHostName();
IPHostEntry ipE=Dns.GetHostByName(sHostName);
IPAddress[]IpA=ipE.AddressList;
对于(int i=0;i

由于一台机器可以有多个ip地址,因此,正确的方法是打开一个连接到internet上主机的套接字,然后检查套接字连接,查看该连接中使用的本地地址


通过检查套接字连接,您将能够考虑奇怪的路由表、多个ip地址和古怪的主机名。上面的主机名的技巧可以工作,但我不认为它是完全可靠的。

如果你正在寻找命令行实用工具IPCONFIG所提供的信息,你可能应该使用Studio.NET.NETWorkDebug命名空间。 此示例代码将枚举所有网络接口,并转储每个适配器的已知地址

using System;
using System.Net;
using System.Net.NetworkInformation;

class Program
{
    static void Main(string[] args)
    {
        foreach ( NetworkInterface netif in NetworkInterface.GetAllNetworkInterfaces() )
        {
            Console.WriteLine("Network Interface: {0}", netif.Name);
            IPInterfaceProperties properties = netif.GetIPProperties();
            foreach ( IPAddress dns in properties.DnsAddresses )
                Console.WriteLine("\tDNS: {0}", dns);
            foreach ( IPAddressInformation anycast in properties.AnycastAddresses )
                Console.WriteLine("\tAnyCast: {0}", anycast.Address);
            foreach ( IPAddressInformation multicast in properties.MulticastAddresses )
                Console.WriteLine("\tMultiCast: {0}", multicast.Address);
            foreach ( IPAddressInformation unicast in properties.UnicastAddresses )
                Console.WriteLine("\tUniCast: {0}", unicast.Address);
        }
    }
}

您可能对UnicastAddress最感兴趣。

使用Dns要求您的计算机在本地Dns服务器上注册,如果您在intranet上,则不一定如此,如果您在家与ISP在一起,则更不可能。它还需要一次网络往返——所有这些都是为了找到关于你自己电脑的信息

正确的方法:

NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
foreach(NetworkInterface  adapter in  nics)
{
    foreach(var x in adapter.GetIPProperties().UnicastAddresses)
    {
        if (x.Address.AddressFamily == AddressFamily.InterNetwork  && x.IsDnsEligible)
        {
                    Console.WriteLine(" IPAddress ........ : {0:x}", x.Address.ToString());
        }
    }
}
(2015年7月31日更新:修复了代码的一些问题)

或者对于那些只喜欢Linq系列的人:

NetworkInterface.GetAllNetworkInterfaces()
    .SelectMany(adapter=> adapter.GetIPProperties().UnicastAddresses)
    .Where(adr=>adr.Address.AddressFamily == AddressFamily.InterNetwork  && adr.IsDnsEligible)
    .Select (adr => adr.Address.ToString());

如果您知道您的计算机有一个或多个IPv4地址,这将提供其中之一:

Dns.GetHostAddresses(Dns.GetHostName())
    .First(a => a.AddressFamily == AddressFamily.InterNetwork).ToString()

GetHostAddresses
通常在调用线程查询DNS服务器时阻止调用线程,如果查询失败,则抛出
SocketException
。我不知道它在查找您自己的主机名时是否跳过网络调用。

您返回的阵列中的哪个IP地址是正确的?GetHostByName显示为已弃用。结束使用:IPAddress[]IPAddress=Dns.GetHostAddresses(strHostName);完成同样的事情。静态IPAddress[]GetAddresses(字符串主机){返回Dns.GetHostAddresses(主机);}嘿,James,从你的回答行第2行,c#编译器说“找不到类型或命名空间”
NetworkInformation
,我已经导入了
System.Net.NetworkInformation
,然后它说“类型或名称空间丢失”,这是怎么回事?@FosterZ:我同意(它不能按原样工作)。我将此代码实现为:
foreach(NIC中的网络接口ni){foreach(ni.GetIPProperties().unicastaddress中的UnicastIPAddressInformation x){if(x.Address.AddressFamily==System.Net.Sockets.AddressFamily.InterNetwork){Console.WriteLine(“IPAddress……。:{0:x}”,x.Address.ToString();}}
,它混合打印出了我的大部分实际IP地址和其他一些非我的IP地址。
Dns.GetHostAddresses(Dns.GetHostName())
    .First(a => a.AddressFamily == AddressFamily.InterNetwork).ToString()