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);
            }
        }
    }
}

相关文章:

  • 2021-05-31
  • 2021-12-28
  • 2021-12-29
  • 2022-01-16
  • 2021-12-10
  • 2022-01-07
猜你喜欢
  • 2021-12-05
  • 2021-12-23
  • 2022-02-22
  • 2022-12-23
  • 2021-09-06
  • 2022-12-23
  • 2021-12-16
相关资源
相似解决方案