如何编程实现修改IP地址、子网掩码、网关、DNS地址等?
2009-04-01 13:29
585 查看
using System; using System.Management; using System.Threading; namespace WmiIpChanger { class IpChanger { [MTAThread] static void Main(string[] args) { ReportIP(); // SwitchToDHCP(); SwitchToStatic(); Thread.Sleep(5000); ReportIP(); Console.WriteLine("end."); } static void SwitchToDHCP() { 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; inPar = mo.GetMethodParameters("EnableDHCP"); outPar = mo.InvokeMethod("EnableDHCP", inPar, null); break; } } static void SwitchToStatic() { 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; inPar = mo.GetMethodParameters("EnableStatic"); inPar["IPAddress"] = new string[] { "192.168.1.1" }; inPar["SubnetMask"] = new string[] { "255.255.255.0" }; outPar = mo.InvokeMethod("EnableStatic", inPar, null); break; } } static void ReportIP() { Console.WriteLine("****** Current IP addresses:"); ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if (!(bool) mo["IPEnabled"]) continue; Console.WriteLine("{0}/n SVC: '{1}' MAC: [{2}]", (string) mo["Caption"], (string) mo["ServiceName"], (string) mo["MACAddress"]); string[] addresses = (string[]) mo["IPAddress"]; string[] subnets = (string[]) mo["IPSubnet"]; Console.WriteLine(" Addresses :"); foreach (string sad in addresses) Console.WriteLine("/t'{0}'", sad); Console.WriteLine(" Subnets :"); foreach (string sub in subnets) Console.WriteLine("/t'{0}'", sub); } } } }
相关文章推荐
- 如何编程实现修改IP地址、子网掩码、网关、DNS地址等?
- 如何编程实现修改本机的IP地址DNS服务器等设置
- 批处理文件快速动态修改IP地址,默认网关和DNS来实现网络切换
- 本地连接的ip地址 子网掩码 默认网关 还有dns服务器地址怎么设置? (转自网易博客)
- linux学习之修改IP地址、网关地址、DNS
- IP地址、子网掩码、网关及DNS地址
- c#修改本地连接工具 ip地址,dns,网关,子网掩码
- 批处理_修改IP地址,子网掩码,默认网关,DNS
- CentOS(RedHat)命令行临时修改IP地址、网关、DNS
- 设备中如何实现类似gopro给iphone分配ip但是不修改dns的功能
- CentOS 修改IP地址, DNS, 网关
- MAC地址、IP地址、子网掩码、域名、DNS、网关
- CentOS 修改IP地址, DNS, 网关
- 如何取得本机网卡的型号,IP地址,子网掩码和网关
- IP地址,子网掩码、默认网关,DNS服务器是什么意思?
- CentOS 修改IP地址DNS,网关
- 用批处理(.BAT)自动设置IP地址、子网掩码、网关、DNS
- Windows下命令行修改IP地址DNS地址
- IP地址、网关地址、子网掩码地址
- TCP/IP协议中的三个参数 (IP地址、子网掩码和网关地址)