• 获取网卡信息


    获取网卡的基本信息:名称、MAC地址、网卡描述信息、IP地址、网关、DNS等。
    基本方法:NetworkInterface类获取gateway和dns信息;System.Net.Dns类获取IP地址。

    示例如下:

    /*
     * Created by SharpDevelop.
     * User: JACK
     * Date: 2011-4-20
     * Time: 18:47
     * 
     * To change this template use Tools | Options | Coding | Edit Standard Headers.
     */
    using System;
    using System.Net.NetworkInformation;

    namespace DemoConsole
    {
        class Program
        {
            public static void Main(string[] args)
            {    
                NetworkInterface[] nsi = NetworkInterface.GetAllNetworkInterfaces();
                foreach (var ni in nsi) {
                    //TODO:if NOT Connected then CONTINUE
                    if (ni.OperationalStatus != OperationalStatus.Up) {
                        continue;
                    }
                    // TODO:Get the Basic Info: Name、GetPhysicalAddress()、Description
                    PhysicalAddress address = ni.GetPhysicalAddress();
                    Console.WriteLine("{0}\t{1}\t{2}",ni.Name,address,ni.Description);
                    // TODO:Get the ip address
                    string hostname = System.Net.Dns.GetHostName();
                    System.Net.IPHostEntry ips = System.Net.Dns.GetHostEntry(hostname);
                    foreach (var ip in ips.AddressList) {
                        Console.WriteLine("IP:\t{0}",ip );
                    }
                    // TODO:gateway:element.GetIPProperties().GatewayAddresses[0].Address 
                    foreach (var gateway in ni.GetIPProperties().GatewayAddresses) {
                        Console.WriteLine("GW:\t{0}",gateway.Address);
                    }
                    //TODO:DNS INFO
                    foreach (var dns in ni.GetIPProperties().DnsAddresses) {
                        Console.WriteLine("DNS:\t{0}",dns);
                    }

                    Console.WriteLine("--==---------------------------------------------------------==--");
                }
                Console.Write("Press any key to continue . . . ");
                Console.ReadKey(true);
            }
        }
    }

  • 相关阅读:
    【转载】淘宝数据魔方技术架构解析
    【转载】IE6 FORM不能提交的解决办法
    【转载】雷人语录
    【转载】如何使员工更敬业
    搜狐开源镜像连接地址
    【转载】IT工作者应具备的素质(精)
    【转载】一些经典的计算机书籍
    【转载】代码审查:大家都应该做的事情
    【转载】编程目标:开发人员如何提高能力
    SQL日志文件长度过大的处理方法
  • 原文地址:https://www.cnblogs.com/flaaash/p/2022694.html
Copyright © 2020-2023  润新知