获取网卡信息

获取网卡的基本信息:名称、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);
        }
    }
}

原文地址:https://www.cnblogs.com/flaaash/p/2022694.html