现在的位置: 首页 > 综合 > 正文

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

2014年09月05日 ⁄ 综合 ⁄ 共 7384字 ⁄ 字号 评论关闭

说明:本人准备写一些C#有关的小技巧系列文章,这些文章含金量并不高,代码难度不大,不过因为问的次数比较多,从而导致本人决定用自己所知的方式写这一系列文章,可以看做“趣味导学”系列吧。

这是一个获取系统所有服务的程序,为了简化程序,代码仍以控制台形式提供,大家可以另外封装自己希望的功能。

核心代码:

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));
            }
        }
    }

在我本机的执行结果如下:
Interface information for zhoufoxcn.sooyie
  Number of interfaces .................... : 2

NVIDIA nForce Networking Controller
===================================
  网络类型 .......................... : Ethernet
  MAC地址 ........................ : 003018ADB71C
  Operational status ...................... : Up
  IP version .............................. : IPv4
Dhcp服务器地址:
255.255.255.255
DNS服务器 :
192.168.3.3
202.99.192.66
网关 :
192.168.3.1
是否被配置为向域名系统 (DNS) 服务器发送名称解析查询:False
是否被配置为自动向域名系统 (DNS) 注册其 IP 地址信息:True
分配给此接口的多路广播地址:
224.0.0.1
Windows Internet 名称服务 (WINS) 服务器的地址:
0.0.0.0
0.0.0.0
  DNS suffix .............................. :
  MTU...................................... : 1500
是否被配置为向域名系统 (DNS) 服务器发送名称解析查询:...........................
.. : False
是否被配置为自动向域名系统 (DNS) 注册其 IP 地址信息: .............. : True
  Receive Only ............................ : False
  Multicast ............................... : True

MS TCP Loopback interface
=========================
  网络类型 .......................... : Loopback
  MAC地址 ........................ :
  Operational status ...................... : Up
  IP version .............................. : IPv4
Dhcp服务器地址:
DNS服务器 :
网关 :
是否被配置为向域名系统 (DNS) 服务器发送名称解析查询:False
是否被配置为自动向域名系统 (DNS) 注册其 IP 地址信息:True
分配给此接口的多路广播地址:
224.0.0.1
Windows Internet 名称服务 (WINS) 服务器的地址:

抱歉!评论已关闭.