您的位置:首页 > 理论基础 > 计算机网络

C# System.Net.NetworkInformation 命名空间学习之NetworkInterface 类获取IP,DNS,Gateway,Mask, MAC等网络相关信息

2014-02-10 10:24 1426 查看
public abstract class NetworkInterface

描述网络接口信息的类,可以从中获取IP,DNS,MAC,Mask,Gateway等信息,还能获取接口状态,是否可用等,

成员方法:

public static NetworkInterface[] GetAllNetworkInterfaces()

静态函数,返回当前电脑里所有可用的网络接口。

例:当调用此函数会返回我电脑上所有网络接口,

===========================================================================

接口列表

11...d4 3d 7e 9d 64 09 ......Realtek PCIe GBE Family Controller

14...00 50 56 c0 00 01 ......VMware Virtual Ethernet Adapter for VMnet1

16...00 50 56 c0 00 08 ......VMware Virtual Ethernet Adapter for VMnet8

1...........................Software Loopback Interface 1

12...00 00 00 00 00 00 00 e0 Microsoft ISATAP Adapter

13...00 00 00 00 00 00 00 e0 Teredo Tunneling Pseudo-Interface

15...00 00 00 00 00 00 00 e0 Microsoft ISATAP Adapter #2

17...00 00 00 00 00 00 00 e0 Microsoft ISATAP Adapter #3

===========================================================================

public abstract IPInterfaceProperties GetIPProperties()


GetAllNetworkInterface() 返回的NetworkInterface 对象集合,

NetWorkInterface.GetIPProperties();

这个函数返回一个IPInterfaceProperties对象,包含了这个网络接口的一些信息,

public abstract class IPInterfaceProperties

这是IPInterfaceProperties类的定义,你不能直接创建此类的实例,

====================================获取网关,DNS,IP,Mask地址,(DNS可能是个多个)==============================

这个类中包括:DnsAddress,GatewayAddress, UnicastAddresses(从中获取Mask地址,IP地址)

====================================获取物理 地址=============================

回到NetworkInterface 类

方法:GetPhysicalAddress() 返回一个网络接口的物理地址对象PhysicalAddress

public abstract PhysicalAddress GetPhysicalAddress()

public class PhysicalAddress

调用PhysicalAddress的方法GetAddressBytes() 返回MAC地址的值,返回的是一个byte[] 数组,一般情况是返回6个字节的数组,存放的是十进制数字,转换成十六进制就可以得到如:D4-3D-7E-9D-64-09 这样的MAC地址,

PhysicalAddress有个静态方法,可以将 D4-3D-7E-9D-64-09 这类的字符串转换成一个PhysicalAddress 对象。

public static PhysicalAddress Parse(
string address
)

address 只接受 数字和大写字母加“-”组成的MAC地址,例:001122334455, 00-11-22-33-44-55, D4-3D-7E-9D-64-09,其他格式都将抛出 FormatException 异常

NetworkInterface 有用属性

Name 网络适配器的名字,比如我们常见的“本地连接”

NetworkInterfaceType 网络接口类型,储存的是System.Net.NetworkInformation.NetworkInterfaceType枚举

值:红色的为我们常用的,
Unknown
Ethernet 以太网

TokenRing
Fddi
BasicIsdn
PrimaryIsdn
Ppp
Loopback 回环127.0.0.1

Ethernet3Megabit
Slip
Atm
GenericModem
FastEthernetT
Isdn
FastEthernetFx
Wireless80211 无线网

AsymmetricDsl
RateAdaptDsl
SymmetricDsl
VeryHighSpeedDsl
IPOverAtm
GigabitEthernet
Tunnel
MultiRateSymmetricDsl
HighPerformanceSerialBus
OperationalStatus 网络连接状态,是一个System.Net.NetworkInformation.OperationalStatus枚举,

值:

Up 启用的

Down 未启用 如果你的本地连接没有插网线,状态就是DOWN

Testing
Unknown
Dormant
NotPresent
LowerLayerDown
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: