Example #1
0
File: Form1.cs Project: zzyyu/SetIP
        private void button1_Click(object sender, EventArgs e)
        {
            //测试
            ManagementBaseObject       myInMBO        = null;
            ManagementBaseObject       myOutMBO       = null;
            ManagementClass            myMClass       = new ManagementClass("Win32_NetWorkAdapterConfiguration");
            ManagementObjectCollection myMOCollection = myMClass.GetInstances();

            foreach (ManagementObject MObject in myMOCollection)
            {
                if (!(bool)MObject["IPEnabled"])
                {
                    continue;
                }
                myInMBO = MObject.GetMethodParameters("EnableStatic");
                myInMBO["IPAddress"]  = new string[] { textBox1.Text };
                myInMBO["SubnetMask"] = new string[] { textBox2.Text };
                myOutMBO = MObject.InvokeMethod("EnableStatic", myInMBO, null);
                //设置网关地址
                myInMBO = MObject.GetMethodParameters("SetGateways");
                myInMBO["DefaultIPGateway"] = new string[] { textBox3.Text };
                myOutMBO = MObject.InvokeMethod("SetGateways", myInMBO, null);
                //设置DNS
                myInMBO = MObject.GetMethodParameters("SetDNSServerSearchOrder");
                myInMBO["DNSServerSearchOrder"] = new string[] { textBox4.Text, textBox5.Text };
                myOutMBO = MObject.InvokeMethod("SetDNSServerSearchOrder", myInMBO, null);
                break;
            }
            ShowInfo();
            MessageBox.Show("IP地址设置成功!");
        }