有关网卡操作的两个对象之一:
Win32_NetworkAdapterConfiguration 网卡配置信息
定义如下:
1 class Win32_NetworkAdapterConfiguration : CIM_Setting 2 { 3 boolean ArpAlwaysSourceRoute; 4 boolean ArpUseEtherSNAP; 5 string Caption;//网卡名称 6 string DatabasePath; 7 boolean DeadGWDetectEnabled; 8 string DefaultIPGateway[];//默认网关 9 uint8 DefaultTOS; 10 uint8 DefaultTTL; 11 string Description; 12 boolean DHCPEnabled; //是否启用DHCP 13 datetime DHCPLeaseExpires; 14 datetime DHCPLeaseObtained; 15 string DHCPServer; 16 string DNSDomain; 17 string DNSDomainSuffixSearchOrder[]; 18 boolean DNSEnabledForWINSResolution; 19 string DNSHostName; 20 string DNSServerSearchOrder[];//DNS(包括主、备) 21 boolean DomainDNSRegistrationEnabled; 22 uint32 ForwardBufferMemory; 23 boolean FullDNSRegistrationEnabled; 24 uint16 GatewayCostMetric[]; 25 uint8 IGMPLevel; 26 uint32 Index; //网卡在所有网卡中的序号 27 uint32 InterfaceIndex; 28 string IPAddress[];//IP地址 29 uint32 IPConnectionMetric; 30 boolean IPEnabled; //是不是启用IP 31 boolean IPFilterSecurityEnabled; 32 boolean IPPortSecurityEnabled; 33 string IPSecPermitIPProtocols[]; 34 string IPSecPermitTCPPorts[]; 35 string IPSecPermitUDPPorts[]; 36 string IPSubnet[]; //子网掩码 37 boolean IPUseZeroBroadcast; 38 string IPXAddress; 39 boolean IPXEnabled; 40 uint32 IPXFrameType[]; 41 uint32 IPXMediaType; 42 string IPXNetworkNumber[]; 43 string IPXVirtualNetNumber; 44 uint32 KeepAliveInterval; 45 uint32 KeepAliveTime; 46 string MACAddress; //MAC地址 47 uint32 MTU; 48 uint32 NumForwardPackets; 49 boolean PMTUBHDetectEnabled; 50 boolean PMTUDiscoveryEnabled; 51 string ServiceName; 52 string SettingID; 53 uint32 TcpipNetbiosOptions; 54 uint32 TcpMaxConnectRetransmissions; 55 uint32 TcpMaxDataRetransmissions; 56 uint32 TcpNumConnections; 57 boolean TcpUseRFC1122UrgentPointer; 58 uint16 TcpWindowSize; 59 boolean WINSEnableLMHostsLookup; 60 string WINSHostLookupFile; 61 string WINSPrimaryServer; 62 string WINSScopeID; 63 string WINSSecondaryServer; 64 };
Win32_NetworkAdapterConfiguration 有如下方法:
| 方法名 | 描述信息 |
|---|---|
| DisableIPSec |
Disables IPsec on this TCP/IP-enabled network adapter. |
| EnableDHCP |
Enables the Dynamic Host Configuration Protocol (DHCP) for service with this network adapter.开启DCHP,使IP自动获取! |
| EnableDNS |
Enables the Domain Name System (DNS) for service on this TCP/IP-bound network adapter. |
| EnableIPFilterSec |
Enables IPsec globally across all IP-bound network adapters. |
| EnableIPSec |
Enables IPsec on this specific TCP/IP-enabled network adapter. |
| EnableStatic |
Enables static TCP/IP addressing for the target network adapter.关闭DHCP,手动设置IP、子网掩码 |
| EnableWINS |
Enables WINS settings specific to TCP/IP, but independent of the network adapter. |
| ReleaseDHCPLease |
Releases the IP address bound to a specific DHCP-enabled network adapter. |
| ReleaseDHCPLeaseAll |
Releases the IP addresses bound to all DHCP-enabled network adapters. |
| RenewDHCPLease |
Renews the IP address on specific DHCP-enabled network adapters. |
| RenewDHCPLeaseAll |
Renews the IP addresses on all DHCP-enabled network adapters. |
| SetArpAlwaysSourceRoute |
Sets the transmission of ARP queries by the TCP/IP. |
| SetArpUseEtherSNAP |
Enables Ethernet packets to use 802.3 SNAP encoding. |
| SetDatabasePath |
Sets the path to the standard Internet database files (HOSTS, LMHOSTS, NETWORKS, and PROTOCOLS). |
| SetDeadGWDetect |
Enables dead gateway detection. |
| SetDefaultTOS |
Obsolete. This method sets the default Type of Service (TOS) value in the header of outgoing IP packets. |
| SetDefaultTTL |
Sets the default Time to Live (TTL) value in the header of outgoing IP packets. |
| SetDNSDomain |
Sets the DNS domain. |
| SetDNSServerSearchOrder |
Sets the server search order as an array of elements.设置DNS服务器。 |
| SetDNSSuffixSearchOrder |
Sets the suffix search order as an array of elements. |
| SetDynamicDNSRegistration |
Indicates dynamic DNS registration of IP addresses for this IP-bound adapter. This method is new for Windows XP. |
| SetForwardBufferMemory |
Specifies how much memory IP allocates to store packet data in the router packet queue. |
| SetGateways |
Specifies a list of gateways for routing packets destined for a different subnet than the one this adapter is connected to.设置默认网关。 |
| SetIGMPLevel |
Sets the extent to which the system supports IP multicasting and participates in the Internet Group Management Protocol. |
| SetIPConnectionMetric |
Sets the routing metric associated with this IP-bound adapter.
|
| SetIPUseZeroBroadcast |
Sets IP zero broadcast usage. |
| SetIPXFrameTypeNetworkPairs |
The Internetwork Packet Exchange (IPX) technology is no longer available for use as of Windows Vista, and this method is obsolete.
|
| SetIPXVirtualNetworkNumber |
The Internetwork Packet Exchange (IPX) technology is no longer available for use as of Windows Vista, and this method is obsolete.
|
| SetKeepAliveInterval |
Sets the interval separating Keep Alive Retransmissions until a response is received. |
| SetKeepAliveTime |
Sets how often TCP attempts to verify that an idle connection is still available by sending a Keep Alive packet. |
| SetMTU |
Sets the default Maximum Transmission Unit (MTU) for a network interface. This method is not supported. |
| SetNumForwardPackets |
Sets the number of IP packet headers allocated for the router packet queue. |
| SetPMTUBHDetect |
Enables detection of Black Hole routers. |
| SetPMTUDiscovery |
Enables Maximum Transmission Unit (MTU) discovery. |
| SetTcpipNetbios |
Sets the default operation of NetBIOS over TCP/IP.
|
| SetTcpMaxConnectRetransmissions |
Sets the number of attempts TCP will retransmit a connect request before aborting. |
| SetTcpMaxDataRetransmissions |
Sets the number of times TCP will retransmit an individual data segment before aborting the connection. |
| SetTcpNumConnections |
Sets the maximum number of connections that TCP may have open simultaneously. |
| SetTcpUseRFC1122UrgentPointer |
Specifies whether TCP uses the RFC 1122 specification for urgent data, or the mode used by Berkeley Software Design (BSD) derived systems. |
| SetTcpWindowSize |
Sets the maximum TCP Receive Window size offered by the system. |
| SetWINSServer |
Sets the primary and secondary Windows Internet Naming Service (WINS) servers on this TCP/IP-bound network adapter. |
以上引自MSDN:http://msdn.microsoft.com/en-us/library/aa394217.aspx
C#代码示例:
1 using System.Management; // System.Management 2 using System.Collections; // System.Collections 3 4 namespace Win32_NetworkAdapterConfiguration 5 { 6 public partial class Form1 : Form 7 { 8 ArrayList AryLst = new ArrayList(); // ArrayList 9 ManagementObject objCls; // ManagementObject 10 string strCls = "Win32_NetworkAdapterConfiguration"; // WMI 命名空间 ( Namespace ) 11 string strNS = "root\\CIMV2"; // WMI 类 (Class) 12 string strIndex; // 用来记录网卡界面 Index 13 Public Form1() 14 { 15 InitializeComponent(); 16 } 17 private void Form1_Load(object sender, EventArgs e) 18 { 19 GetAdtInfo(); // 获取网卡信息 20 } 21 private void GetAdtInfo() 22 { 23 // 指定查询网卡信息 ( IPEnabled 为 True 的 ) 24 string strQry = "Select * from Win32_NetworkAdapterConfiguration where IPEnabled=True"; 25 // ManagementObjectSearcher 类 ,根据指定的查询获取物理控件的集合。 26 ManagementObjectSearcher objSc = new ManagementObjectSearcher(strQry); 27 // 用foreach获取元素 28 foreach (ManagementObject objQry in objSc.Get()) 29 { 30 AryLst.Add(objQry["Index"]); 31 ComboBox.Items.Add(objQry["Description"]); 32 } 33 } 34 // 35 private void button1_Click(object sender, EventArgs e) 36 { 37 string strIP = "192.168.1.3"; // IP地址 38 string strSubmask = "255.255.255.0"; // 子网掩码 39 string strGateway = "192.168.1.1"; // 网关 40 string strDNS1 = "168.95.1.1"; // 首选DNS 41 string strDNS2 = "168.95.192.1"; // 备用DNS 42 // 调用SetNetCfg()方法设定网卡信息 43 SetNetCfg(strIP, strSubmask, strGateway, strDNS1, strDNS2); 44 } 45 public void SetNetCfg(string strIP, string strSubmask, string strGateway, string strDNS1, string strDNS2) 46 { 47 // 建立 ManagementObject 物件 ( Scope , Path , options ) 48 objCls = new ManagementObject(strNS, strCls + ".INDEX=" + strIndex, null); 49 ManagementBaseObject objInPara; // 50 objInPara = objCls.GetMethodParameters("EnableStatic"); 51 objInPara["IPAddress"] = new string[] { strIP }; // 设置IP 52 objInPara["SubnetMask"] = new string[] { strSubmask }; // 设置子网掩码 53 objCls.InvokeMethod("EnableStatic", objInPara, null); 54 objInPara = objCls.GetMethodParameters("SetGateways"); 55 objInPara["DefaultIPGateway"] = new string[] { strGateway }; // 设置网关 56 objCls.InvokeMethod("SetGateways", objInPara, null); 57 objInPara = objCls.GetMethodParameters("SetDNSServerSearchOrder"); 58 objInPara["DNSServerSearchOrder"] = new string[] { strDNS1, strDNS2 }; // 设置DNS 59 objCls.InvokeMethod("SetDNSServerSearchOrder", objInPara, null); 60 // GetMethodParameters 方法 : 用來取得 SetDNSServerSearchOrder 的参数列表。 61 // InvokeMethod 方法 : 调用。 62 } 63 // Button2 使用 DHCP自动获取 IP 64 private void button2_Click(object sender, EventArgs e) 65 { 66 SetAuto(); // 调用SetAuto方法 67 } 68 public void SetAuto() 69 { 70 // 建立 ManagementObject 对象 ( Scope , Path , options ) 71 objCls = new ManagementObject(strNS, strCls + ".INDEX=" + strIndex, null) 72 // InvokeMethod 方法 : 在对象上调用方法。 73 objCls.InvokeMethod("EnableDHCP", null); // 设置自动获取 IP 74 objCls.InvokeMethod("ReleaseDHCPLease", null); // 释放 IP 75 objCls.InvokeMethod("RenewDHCPLease", null); // 重新取得 IP 76 } 77 private void ComboBox_SelectedIndexChanged(object sender, EventArgs e) 78 { 79 strIndex = (ComboBox.SelectedIndex + 1).ToString(); 80 } 81 } 82 }
上面代码来自:http://www.cnblogs.com/sunnyliu357/archive/2012/03/06/2381400.html