【C#】设置有线网卡IP地址,子网掩码,网关,DNS

方法

    public partial class ComputerInfo
    {
        /// 
        /// 设置IP地址,子网掩码,网关,DNS
        /// 
        public static List SetIpAddressSubMaskDnsGeteway(string ipAddress, string subMask, string geteWay, string dnsMain, string dnsBackup)
        {
            List networkAdapterInfos = new List();
            //var str = new StringBuilder();

            ManagementBaseObject inPar = null;
            ManagementBaseObject outPar = null;
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                //屏蔽代码,避免出现无法修改未连接的有线网卡驱动
                //if (!(bool)mo["IPEnabled"])
                //    continue;
                networkAdapterInfos.Add(new NetworkAdapterInfo()
                {
                    Description = mo["Description"].ToString(),
                    IPEnabled = mo["IPEnabled"].ToString(),
                });

                //判断有线网卡
                var description = mo["Description"].ToString();
                //有线网卡
                //Realtek PCIe GbE Family Controller
                //无线网卡
                //Qualcomm QCA9565 802.11b/g/n Wireless Adapter
                //Realtek RTL8852AE WiFi 6 802.11ax PCle Adapter
                //Intel(R) Wi-Fi 6AX201 160MHz
                if (description.Contains("PCI") && new List() { "Wireless", "WiFi", "Wi-Fi", "WLAN" }.Exists(w => description.Contains(w)) == false)
                {
                    //设置ip地址和子网掩码 
                    inPar = mo.GetMethodParameters("EnableStatic");
                    inPar["IPAddress"] = new string[] { ipAddress };
                    inPar["SubnetMask"] = new string[] { subMask };
                    outPar = mo.InvokeMethod("EnableStatic", inPar, null);

                    //设置网关地址 
                    inPar = mo.GetMethodParameters("SetGateways");
                    inPar["DefaultIPGateway"] = new string[] { geteWay };
                    outPar = mo.InvokeMethod("SetGateways", inPar, null);

                    //设置DNS 
                    inPar = mo.GetMethodParameters("SetDNSServerSearchOrder");
                    inPar["DNSServerSearchOrder"] = new string[] { dnsMain, dnsBackup };
                    outPar = mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null);
                }

                //foreach (var item in mo.Properties)
                //{
                //    str.Append($"{item.Name}:{item.Value}\r\n");
                //}
            }

            //var info = str.ToString();
            return networkAdapterInfos;
        }

    }

例子

var networkAdapterInfos = ComputerInfo.SetIpAddressSubMaskDnsGeteway("192.168.11.20", "255.255.255.0", "192.168.11.1", "192.168.11.1", "8.8.8.8");

参考:

1.区分有线网卡和无线网卡:https://www.cnblogs.com/CPFlying/archive/2010/03/14/1685733.html

2.设置ip:c# 如何对网络信息进行相关设置(ip,dns,网关等)_C#教程_脚本之家

你可能感兴趣的:(C#,C#)