C#小技巧系列之四:获取本地网络信息

本文转载自http://blog.csdn.net/zhoufoxcn

using System;
using System.Collections.Generic;
using System.Data.Sql;
using System.Net;
using System.Net.NetworkInformation;

namespace AllSqlServer
{
    class Program
    {
        static void Main(string[] args)
        {
             ShowNetworkInterfaces();

            Console.ReadLine();
        }

        public static void ShowNetworkInterfaces()
        {
            IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            Console.WriteLine("Interface information for {0}.{1}     ",
                    computerProperties.HostName, computerProperties.DomainName);
            if (nics == null || nics.Length < 1)
            {
                Console.WriteLine("  No network interfaces found.");
                return;
            }

            Console.WriteLine("  Number of interfaces .................... : {0}", nics.Length);
            foreach (NetworkInterface adapter in nics)
            {
                IPInterfaceProperties properties = adapter.GetIPProperties();
                Console.WriteLine();
                Console.WriteLine(adapter.Description);
                Console.WriteLine(String.Empty.PadLeft(adapter.Description.Length, '='));
                Console.WriteLine("  网络类型 .......................... : {0}", adapter.NetworkInterfaceType);
                Console.WriteLine("  MAC地址 ........................ : {0}",
                           adapter.GetPhysicalAddress().ToString());
                Console.WriteLine("  Operational status ...................... : {0}",
                    adapter.OperationalStatus);
                string versions = "";

                if (adapter.Supports(NetworkInterfaceComponent.IPv4))
                {
                    versions = "IPv4";
                }
                if (adapter.Supports(NetworkInterfaceComponent.IPv6))
                {
                    if (versions.Length > 0)
                    {
                        versions += " ";
                    }
                    versions += "IPv6";
                }
                Console.WriteLine("  IP version .............................. : {0}", versions);
                ShowIPAddresses(properties);

                if (adapter.NetworkInterfaceType == NetworkInterfaceType.Loopback)
                {
                    continue;
                }
                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;
                        if (winsServers.Count > 0)
                        {
                            label = "  WINS Servers ............................ :";
                            ShowIPAddresses(label, winsServers);
                        }
                    }
                }

                Console.WriteLine("是否被配置为向域名系统 (DNS) 服务器发送名称解析查询:............................. : {0}",
                    properties.IsDnsEnabled);
                Console.WriteLine("是否被配置为自动向域名系统 (DNS) 注册其 IP 地址信息: .............. : {0}",
                    properties.IsDynamicDnsEnabled);
                Console.WriteLine("  Receive Only ............................ : {0}",
                    adapter.IsReceiveOnly);
                Console.WriteLine("  Multicast ............................... : {0}",
                    adapter.SupportsMulticast);

                Console.WriteLine();
            }
        }


        private static void ShowIPAddresses(IPInterfaceProperties properties)
        {
            Console.WriteLine("Dhcp服务器地址:");
            ShowIPAddressCollection(properties.DhcpServerAddresses);
            Console.WriteLine("DNS服务器 :");
            ShowIPAddressCollection(properties.DnsAddresses);
            Console.WriteLine("网关 :");
            ShowGatewayIPAddressInformationCollection(properties.GatewayAddresses);
            Console.WriteLine("是否被配置为向域名系统 (DNS) 服务器发送名称解析查询:" + properties.IsDnsEnabled.ToString());
            Console.WriteLine("是否被配置为自动向域名系统 (DNS) 注册其 IP 地址信息:" + properties.IsDynamicDnsEnabled.ToString());
            Console.WriteLine("分配给此接口的多路广播地址:");
            ShowMulticastIPAddressInformationCollection(properties.MulticastAddresses);
            Console.WriteLine("Windows Internet 名称服务 (WINS) 服务器的地址:");
            ShowIPAddressCollection(properties.WinsServersAddresses);
        }

        private static void ShowIPAddresses(string label,IPAddressCollection winsServers)
        {
            Console.WriteLine("-----"+label+"-----");
            ShowIPAddressCollection(winsServers);
        }

        private static void ShowIPAddressInformationCollection(IPAddressInformationCollection collection)
        {
            for (int i = 0; i < collection.Count; i++)
            {
                Console.WriteLine(GetIPAddressInfo(collection[i].Address));
            }
        }

        private static void ShowIPAddressCollection(IPAddressCollection collection)
        {
            for (int i = 0; i < collection.Count; i++)
            {
                Console.WriteLine(GetIPAddressInfo(collection[i]));
            }
        }

        private static void ShowMulticastIPAddressInformationCollection(MulticastIPAddressInformationCollection collection)
        {
            for (int i = 0; i < collection.Count; i++)
            {
                Console.WriteLine(GetIPAddressInfo(collection[i].Address));
            }
        }

        private static string GetIPAddressInfo(IPAddress address)
        {
            byte[] bytes = address.GetAddressBytes();
            string ipString = null;
            for(int i=0;i<bytes.Length-1;i++)
            {
                ipString += bytes[i] + ".";
            }
            return ipString + bytes[bytes.Length - 1];
        }

        private static void ShowGatewayIPAddressInformationCollection(GatewayIPAddressInformationCollection collection)
        {
            for (int i = 0; i < collection.Count; i++)
            {
                Console.WriteLine(GetIPAddressInfo(collection[i].Address));
            }
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值