zoukankan      html  css  js  c++  java
  • 获取网卡信息

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

  • 相关阅读:
    PHP数组(数组正则表达式、数组、预定义数组)
    面向对象。OOP三大特征:封装,继承,多态。 这个讲的是【封存】
    uvalive 3938 "Ray, Pass me the dishes!" 线段树 区间合并
    LA4329 Ping pong 树状数组
    HDU 1257 最少拦截系统
    HDU 1260 Tickets
    codeforce 621D
    codeforce 621C Wet Shark and Flowers
    codeforce 621B Wet Shark and Bishops
    codeforce 621A Wet Shark and Odd and Even
  • 原文地址:https://www.cnblogs.com/flaaash/p/2022694.html
Copyright © 2011-2022 走看看