在.NET中以编程方式配置网络适配器的最佳方法

我有一个用C#编写的应用程序,需要能够在Windows中配置网络适配器。 我有这个基本上通过WMI工作,但有一些我不喜欢的解决方案:有时设置似乎没有坚持,并且当没有插入网络电缆时,WMI返回错误方法,所以我不知道他们是否真的成功了。

我需要能够配置通过网络连接提供的所有设置 – 属性 – TCP / IP屏幕。

最好的方法是什么?

您可以使用Process来触发netsh命令来设置网络对话框中的所有属性。

例如:在适配器上设置静态ipaddress

 netsh interface ip set address "Local Area Connection" static 192.168.0.10 255.255.255.0 192.168.0.1 1 

要将它设置为dhcp,你可以使用它

 netsh interface ip set address "Local Area Connection" dhcp 

要从C#做到这一点

 Process p = new Process(); ProcessStartInfo psi = new ProcessStartInfo("netsh", "interface ip set address \"Local Area Connection\" static 192.168.0.10 255.255.255.0 192.168.0.1 1"); p.StartInfo = psi; p.Start(); 

设置为静态可能需要几秒钟才能完成,因此如果需要,请确保等待进程退出。

我可以告诉你木马的做法,在完成几个之后必须清理,就是在HKEY_LOCAL_MACHINE下设置注册表项。 他们设置的主要是DNS,并且这种方法绝对坚持,任何曾经受到感染且无法再访问windowsupdate.com,mcafee.com等的人都可以certificate这一点。

使用我的代码SetIpAddress和SetDHCP

  ///  /// Sets the ip address. ///  /// Name of the nic. /// The ip address. /// The subnet mask. /// The gateway. /// The DNS1. /// The DNS2. ///  public static bool SetIpAddress( string nicName, string ipAddress, string subnetMask, string gateway = null, string dns1 = null, string dns2 = null) { ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces(); NetworkInterface networkInterface = interfaces.FirstOrDefault(x => x.Name == nicName); string nicDesc = nicName; if (networkInterface != null) { nicDesc = networkInterface.Description; } foreach (ManagementObject mo in moc) { if ((bool)mo["IPEnabled"] == true && mo["Description"].Equals(nicDesc) == true) { try { ManagementBaseObject newIP = mo.GetMethodParameters("EnableStatic"); newIP["IPAddress"] = new string[] { ipAddress }; newIP["SubnetMask"] = new string[] { subnetMask }; ManagementBaseObject setIP = mo.InvokeMethod("EnableStatic", newIP, null); if (gateway != null) { ManagementBaseObject newGateway = mo.GetMethodParameters("SetGateways"); newGateway["DefaultIPGateway"] = new string[] { gateway }; newGateway["GatewayCostMetric"] = new int[] { 1 }; ManagementBaseObject setGateway = mo.InvokeMethod("SetGateways", newGateway, null); } if (dns1 != null || dns2 != null) { ManagementBaseObject newDns = mo.GetMethodParameters("SetDNSServerSearchOrder"); var dns = new List(); if (dns1 != null) { dns.Add(dns1); } if (dns2 != null) { dns.Add(dns2); } newDns["DNSServerSearchOrder"] = dns.ToArray(); ManagementBaseObject setDNS = mo.InvokeMethod("SetDNSServerSearchOrder", newDns, null); } } catch { return false; } } } return true; } ///  /// Sets the DHCP. ///  /// Name of the nic. public static bool SetDHCP(string nicName) { ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces(); NetworkInterface networkInterface = interfaces.FirstOrDefault(x => x.Name == nicName); string nicDesc = nicName; if (networkInterface != null) { nicDesc = networkInterface.Description; } foreach (ManagementObject mo in moc) { if ((bool)mo["IPEnabled"] == true && mo["Description"].Equals(nicDesc) == true) { try { ManagementBaseObject newDNS = mo.GetMethodParameters("SetDNSServerSearchOrder"); newDNS["DNSServerSearchOrder"] = null; ManagementBaseObject enableDHCP = mo.InvokeMethod("EnableDHCP", null, null); ManagementBaseObject setDNS = mo.InvokeMethod("SetDNSServerSearchOrder", newDNS, null); } catch { return false; } } } return true; } 

借助@PahnB的答案帮助

 NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); Process p = new Process(); ProcessStartInfo psi = new ProcessStartInfo("netsh", "interface ip set address " + nics[0].Name + " static 192.168." + provider_ip + "." + index + " 255.255.255.0 192.168." + provider_ip + ".1 1"); p.StartInfo = psi; p.StartInfo.Verb = "runas"; p.Start(); 

结帐这个应用程序。 它是设置wifi和以太网ips的完整应用程序

https://github.com/kamran7679/ConfigureIP