• 获取网卡信息


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

  • 相关阅读:
    python
    在liunx环境下安装python
    解决用navicate远程连接数据库出现1045
    mysql的监控及优化
    mysql基础
    linux基础学习
    Effective c++ 第一章 让自己习惯C++
    读前感
    socket编程:客户端与服务器间的连接以及各函数的用法
    生成任意区间的随机数
  • 原文地址:https://www.cnblogs.com/flaaash/p/2022694.html
Copyright © 2020-2023  润新知