WPF NetworkInterface


public static void DisplayTypeAndAddress()
        {
            bool bAvailable = NetworkInterface.GetIsNetworkAvailable();
            IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            Console.WriteLine("Interface information for {0}.{1}     ",
                    computerProperties.HostName, computerProperties.DomainName);

            string physicalAddress = "";
            foreach (NetworkInterface adapter in nics)
            {
                IPInterfaceProperties properties = adapter.GetIPProperties();
                Console.WriteLine(adapter.Description);
                Console.WriteLine(String.Empty.PadLeft(adapter.Description.Length, '='));
                Console.WriteLine("  Interface type .......................... : {0}", adapter.NetworkInterfaceType);//网络类型

                physicalAddress = adapter.GetPhysicalAddress().ToString();
                Console.WriteLine("  Physical Address ........................ : {0}", physicalAddress);
                Console.WriteLine("  Is receive only.......................... : {0}", adapter.IsReceiveOnly);
                Console.WriteLine("  Multicast................................ : {0}", adapter.SupportsMulticast);
                Console.WriteLine();

                string versions = "";
                string ip = "";
                if (adapter.Supports(NetworkInterfaceComponent.IPv4))
                {
                    versions = "IPv4";
                    //IPAddressCollection winsServers = properties.WinsServersAddresses;
                    //foreach (IPAddress ipadd in winsServers)
                    //{
                    //    AddressFamily af = ipadd.AddressFamily;
                    //    ip = ipadd.ToString();
                    //}
                }
                
                if (adapter.Supports(NetworkInterfaceComponent.IPv6))
                {
                    if (versions.Length > 0)
                    {
                        versions += " ";
                    }
                    versions += "IPv6";
                }

                string id = adapter.Id;

                /* 判断adapter是否联网
                 * 1、NetworkInterfaceType不是Loopback
                 * 2、NetworkInterfaceType不是Tunnel
                 * 3、OperationalStatus为UP
                 */
                string connectionStatus = "";
                if (adapter.NetworkInterfaceType == NetworkInterfaceType.Loopback)//
                {
                    continue;
                }


                //A network connection is considered to be available if any network interface 
                //is marked "up" and is not a loopback or tunnel interface.
                OperationalStatus status = adapter.OperationalStatus;
                if (OperationalStatus.Up == status && NetworkInterfaceType.Tunnel != adapter.NetworkInterfaceType)//
                {
                    connectionStatus = "Connected";
                }
                else
                {
                    connectionStatus = "UnConnected";
                }


                Console.WriteLine("  DNS suffix .............................. : {0}", properties.DnsSuffix);
                string label;
                if (adapter.Supports(NetworkInterfaceComponent.IPv4))
                {
                    IPv4InterfaceProperties ipv4 = properties.GetIPv4Properties();
                    Console.WriteLine("  MTU...................................... : {0}", ipv4.Mtu);
                    if (ipv4.UsesWins)
                    {
                        IPAddressCollection winsServers = properties.WinsServersAddresses;
                        foreach (IPAddress ipadd in winsServers)
                        {
                            ip = ipadd.ToString();
                        }
                    }
                }

                Console.WriteLine("  DNS enabled ............................. : {0}",
                    properties.IsDnsEnabled);
                Console.WriteLine("  Dynamically configured DNS .............. : {0}",
                    properties.IsDynamicDnsEnabled);
                Console.WriteLine("  Receive Only ............................ : {0}",
                    adapter.IsReceiveOnly);
                Console.WriteLine("  Multicast ............................... : {0}",
                    adapter.SupportsMulticast);


                IPv4InterfaceStatistics iss = adapter.GetIPv4Statistics();

                Console.WriteLine();
            }
        }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值