Click here to Skip to main content
13,256,323 members (51,881 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I've been trying to write a code to change my IP address, but when I run my code the IP address is the same as before. can anybody help me to understand what's wrong?

        private void Form1_Load(object sender, EventArgs e)
            textBox1.Text = getip();
            textBox2.Text = getip();
        public string getip()
            IPHostEntry host;
            string localIP = "?";
            host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in host.AddressList)
                if (ip.AddressFamily.ToString() == "InterNetwork")
                    localIP = ip.ToString();
            return localIP;
        public void setIP(string ip_address, string subnet_mask)
            ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection objMOC = objMC.GetInstances();
            foreach (ManagementObject objMO in objMOC)
                if ((bool)objMO["IPEnabled"])
                    ManagementBaseObject setIP;
                    ManagementBaseObject newIP =
                    newIP["IPAddress"] = new string[] { ip_address };
                    newIP["SubnetMask"] = new string[] { subnet_mask };
                    setIP = objMO.InvokeMethod("EnableStatic", newIP, null);
Posted 20-Sep-12 0:01am
amirmohamad 20-Sep-12 5:32am
be nazar moshkeli nadare!!!
shayad aslan nashe IP ro avaz koni
mehdi_k 20-Sep-12 5:37am
osolan bayad ba in code beshe!
nemifahmam cheshe.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

This is the right way! but check "ipenabled" criteria of the NIC

newIP[ "IPAddress" ] = IpAddresses.Split( ',' );
newIP[ "IPAddress" ] = IpAddresses.Split( ',' );
newIP[ "SubnetMask" ] = new string[] { SubnetMask };
setIP = mo.InvokeMethod( "EnableStatic", newIP, null);
mehdi_k 20-Sep-12 8:20am
thanks, but no change.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.171114.1 | Last Updated 20 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100