今天就跟大家聊聊有关使用C# 实现操作网络适配器的示例,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
成都服务器托管,创新互联公司提供包括服务器租用、四川绵阳服务器托管、带宽租用、云主机、机柜租用、主机租用托管、CDN网站加速、主机域名等业务的一体化完整服务。电话咨询:189820811081、对网络适配器进行配置
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); //设置网关 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; }