今天就跟大家聊聊有關使用C# 實現(xiàn)操作網(wǎng)絡適配器的示例,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
成都服務器托管,創(chuàng)新互聯(lián)公司提供包括服務器租用、四川綿陽服務器托管、帶寬租用、云主機、機柜租用、主機租用托管、CDN網(wǎng)站加速、主機域名等業(yè)務的一體化完整服務。電話咨詢:189820811081、對網(wǎng)絡適配器進行配置
string ethernetIP = System.Configuration.ConfigurationManager.AppSettings["LocalEthernetIP"].Trim().ToString(); string ethernetSubnetMask = System.Configuration.ConfigurationManager.AppSettings["LocalEthernetSubnetMask"].Trim().ToString(); string ethernetGateway = System.Configuration.ConfigurationManager.AppSettings["LocalEthernetGateway"].Trim().ToString(); string ethernetDNS = System.Configuration.ConfigurationManager.AppSettings["LocalEthernetDNS"].Trim().ToString(); ManagementBaseObject outPar = null; ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if (!(bool)mo["IPEnabled"]) { continue; } //設置IP地址 ManagementBaseObject inPar = mo.GetMethodParameters("EnableStatic"); if (!string.IsNullOrEmpty(ethernetIP)) { inPar["IPAddress"] = new string[] { ethernetIP }; } if (!string.IsNullOrEmpty(ethernetSubnetMask)) { inPar["SubnetMask"] = new string[] { ethernetSubnetMask }; } outPar = mo.InvokeMethod("EnableStatic", inPar, null); //設置網(wǎng)關 inPar = mo.GetMethodParameters("SetGateways"); if (!string.IsNullOrEmpty(ethernetGateway)) { inPar["DefaultIPGateway"] = new string[] { ethernetGateway }; outPar = mo.InvokeMethod("SetGateways", inPar, null); } //設置DNS inPar = mo.GetMethodParameters("SetDNSServerSearchOrder"); if (!string.IsNullOrEmpty(ethernetDNS)) { inPar["DNSServerSearchOrder"] = new string[] { ethernetDNS }; outPar = mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null); } break; }