/// <summary> /// 对指定网卡进行配置 /// </summary> /// <param name="adapterName">网卡名称</param> /// <param name="config">网络配置</param> public static void SetAdapterConfiguration(string adapterName, NetworkConfig config) { var wmi = new ManagementClass("Win32_NetworkAdapterConfiguration"); var moc = wmi.GetInstances(); var found = false; foreach (var m in moc) { var mo = m as ManagementObject; foreach (var p in mo.Properties) { if (p.Value != null) { var desc = p.Value.ToString(); if (p.Name.Equals("Description") && adapterName.Equals(desc)) { found = true; break; } } } if (found) { if (IsValidIPAddr(config.IP) && IsValidMask(config.Mask)) { var parameter = mo.GetMethodParameters("EnableStatic"); parameter["IPAddress"] = new string[] { config.IP }; parameter["SubnetMask"] = new string[] { config.Mask }; mo.InvokeMethod("EnableStatic", parameter, null); } else { // IP地址/子网掩码 是必须设置的 throw new Exception($"Invalid IP/Mask: IP = {config.IP}, Mask = {config.Mask}"); } if (IsValidIPAddr(config.Gateway)) { var parameter = mo.GetMethodParameters("SetGateways"); parameter["DefaultIPGateway"] = new string[] { config.Gateway }; mo.InvokeMethod("SetGateways", parameter, null); } else { //throw new Exception($"Invalid Gateway: Gateway = {config.Gateway}"); } if (IsValidIPAddr(config.DNS) && IsValidIPAddr(config.DNS2)) { var parameter = mo.GetMethodParameters("SetDNSServerSearchOrder"); parameter["DNSServerSearchOrder"] = new string[] { config.DNS, config.DNS2 }; mo.InvokeMethod("SetDNSServerSearchOrder", parameter, null); } else { //throw new Exception($"Invalid DNS: DNS = {config.DNS}, DNS2 = {config.DNS2}"); } break; } } }
/// <summary> /// 查询所有网卡信息 /// </summary> private static void SearchAllNetworkAdapters() { if (adapterNames == null) { adapterNames = new List <string>(); var query = "SELECT * From Win32_NetworkAdapter"; var searcher = new ManagementObjectSearcher(query); var collection = searcher.Get(); foreach (var obj in collection) { var mo = obj as ManagementObject; foreach (var p in mo.Properties) { if (p.Name.Equals("NetConnectionID") && p.Value != null) { var name = obj["Name"].ToString(); adapterNames.Add(name); } } } } if (networkConfigurations == null) { networkConfigurations = new Dictionary <string, NetworkConfig>(); } else { networkConfigurations.Clear(); } var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces(); foreach (var nwi in networkInterfaces) { if (adapterNames.Contains(nwi.Description)) { var config = new NetworkConfig(); config.SetAdapterName(nwi.Description); config.SetInterfaceType(nwi.NetworkInterfaceType); var ipp = nwi.GetIPProperties(); var ipCollection = ipp.UnicastAddresses; foreach (var ipAddr in ipCollection) { if (ipAddr.Address.AddressFamily == AddressFamily.InterNetwork) { config.IP = ipAddr.Address.ToString(); config.Mask = ipAddr.IPv4Mask.ToString(); if (ipp.GatewayAddresses.Count > 0) { config.Gateway = ipp.GatewayAddresses[0].Address.ToString(); } break; } } networkConfigurations.Add(nwi.Description, config); } } foreach (var name in adapterNames) { if (!networkConfigurations.ContainsKey(name)) { var config = new NetworkConfig(); config.SetAdapterName(name); config.IP = "N/A"; networkConfigurations.Add(name, config); } } }