本文转载自http://blog.csdn.net/zhoufoxcn
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));
}
}
}
}