1. Configure the network adapter
string ethernetIP = ["LocalEthernetIP"].Trim().ToString(); string ethernetSubnetMask = ["LocalEthernetSubnetMask"].Trim().ToString(); string ethernetGateway = ["LocalEthernetGateway"].Trim().ToString(); string ethernetDNS = ["LocalEthernetDNS"].Trim().ToString(); ManagementBaseObject outPar = null; ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = (); foreach (ManagementObject mo in moc) { if (!(bool)mo["IPEnabled"]) { continue; } //Set IP address ManagementBaseObject inPar = ("EnableStatic"); if (!(ethernetIP)) { inPar["IPAddress"] = new string[] { ethernetIP }; } if (!(ethernetSubnetMask)) { inPar["SubnetMask"] = new string[] { ethernetSubnetMask }; } outPar = ("EnableStatic", inPar, null); //Set up the gateway inPar = ("SetGateways"); if (!(ethernetGateway)) { inPar["DefaultIPGateway"] = new string[] { ethernetGateway }; outPar = ("SetGateways", inPar, null); } //Set DNS inPar = ("SetDNSServerSearchOrder"); if (!(ethernetDNS)) { inPar["DNSServerSearchOrder"] = new string[] { ethernetDNS }; outPar = ("SetDNSServerSearchOrder", inPar, null); } break; }
2. Obtain network adapter and other related information
//Get information about network card NetworkInterface[] nics = (); foreach (NetworkInterface adapter in nics) { //Discern whether it is an Ethernet card //Wireless80211 Wireless Network Card Ppp Broadband Connection //Ethernet Ethernet card //The space here is limited and a few commonly used posts. For other return values, please use Baidu! if ( == ) { //Get the Ethernet card network interface information IPInterfaceProperties ip = (); //Get unicast address set UnicastIPAddressInformationCollection ipCollection = ; foreach (UnicastIPAddressInformation ipadd in ipCollection) { //InterNetwork IPV4 address InterNetworkV6 IPV6 address //Max MAX address if ( == ) //Judge whether it is ipv4 = ();//Get ip } } }
NetworkInterfaceTypeThe return value is as follows(The following table is frommsdn): Member name illustrate Unknown The interface type is unknown。 Ethernet Network interfaces use Ethernet connection。 Ethernet is in IEEE standard 802.3 Definition in。 TokenRing Network interfaces are connected using token rings。 Token ring is IEEE standard 802.5 Definition in。 Fddi The network interface uses a distributed fiber data interface (FDDI) connect。 FDDI 是一组用于局域网中光纤线路上的数据传输的standard。 BasicIsdn Network interface uses basic rate interface integrated service digital network (ISDN) connect。 ISDN 是一组通过电话线Transfer data的standard。 PrimaryIsdn Network interface uses main rate interface integrated service digital network (ISDN) connect。 ISDN 是一组通过电话线Transfer data的standard。 Ppp Network interfaces use point-to-point protocols (PPP) connect。 PPP It is a protocol for data transmission using serial devices。 Loopback The network interface is a loopback adapter。 Such interfaces are usually used for testing;Not sending traffic through cables。 Ethernet3Megabit Network interface uses Ethernet 3 Megabits/Secondconnect。 This version of Ethernet is IETF RFC 895 Definition in。 Slip The network interface uses serial lines Internet protocol (SLIP) connect。 SLIP exist IETF RFC 1055 Definition in。 Atm The network interface uses asynchronous transmission mode (ATM) Transfer data。 GenericModem The network interface uses a modem。 FastEthernetT The network interface uses twisted pair cables Fast Ethernet connect,Its data rate is 100 Megabits/Second。 此connect类型也称为 100Base-T。 Isdn The network interface is used as ISDN and X.25 protocol配置的connect。 X.25 Allows computers on public networks to communicate using intermediate computers。 FastEthernetFx The network interface uses fiber-based Fast Ethernet connect,Its data rate is 100 Megabits/Second。 此connect类型也称为 100Base-FX。 Wireless80211 Wireless network interface LAN connect(IEEE 802.11 standard)。 AsymmetricDsl Network interface uses asymmetric digital user lines (ADSL)。 RateAdaptDsl Network interfaces use rate adaptive digital user lines (RADSL)。 SymmetricDsl Network interface uses symmetric digital subscriber lines (SDSL)。 VeryHighSpeedDsl Network interface uses ultra-high data rate digital user lines (VDSL)。 IPOverAtm Network interface usage Internet protocol (IP) With asynchronous transmission mode (ATM) 相结合来Transfer data。 GigabitEthernet Network interface usage Gigabit Ethernet connect,Its data rate is 1,000 Megabits/Second(1 Gibitt/Second)。 Tunnel Network interface usage隧道connect。 MultiRateSymmetricDsl Network interface usage多速率数字用户线路。 HighPerformanceSerialBus Network interface usage高性能串行总线。 AddressFamilyThe return value is as follows(The following table is frommsdn): Member name称 illustrate AppleTalk AppleTalk address。 Atm This machine ATM 服务address。 Banyan Banyan address。 Ccitt CCITT protocol(like X.25)的address。 Chaos MIT CHAOS protocol的address。 Cluster Microsoft 群集产品的address。 DataKit Datakit protocol的address。 DataLink 直接数据链接接口address。 DecNet DECnet address。 Ecma European Computer Manufacturers Association (ECMA) address。 FireFox FireFox address。 HyperChannel NSC Hyperchannel address。 Ieee12844 IEEE 1284.4 工作组address。 ImpLink ARPANET IMP address。 InterNetwork IP Version 4 的address。 InterNetworkV6 IP Version 6 的address。 Ipx IPX or SPX address。 Irda IrDA address。 Iso ISO protocol的address。 Lat LAT address。 Max MAX address。 NetBios NetBios address。 NetworkDesignersSupports network designer OSI 网关的protocol的address。 NS Xerox NS protocol的address。 Osi OSI protocol的address。 Pup PUP protocol的address。 Sna IBM SNA address。 Unix Unix 本地到主机address。 Unknown 未知的address族。 Unspecified 未指定的address族。 VoiceView VoiceView address。
The above is the detailed content of the example of C# operating network adapter. For more information about C# operating network adapter, please pay attention to my other related articles!