Compartilhar via


Programmatically Changing Your IP Address

A friend needs to change back and forth between a DHCP network and a static IP network. Rather than teach her how to go in and configure the settings manually, I wanted to write her a program that would automate everything.

I found the registry settings that control everything, but after changing them, you need to reboot. There had to be a better way.

I finally stumbled across netsh and it's a lot more powerful than I knew. It turns out, my problem can be solved with a batch file. To set the network interface to use DHCP, run this:

netsh interface ip set address name="Local Area Connection" dhcp
netsh interface ip set dns name="Local Area Connection" dhcp

To flip over to a static IP configuration, use something like this:

netsh interface ip set address name="Local Area Connection" static 192.168.1.12 255.255.255.0 192.168.1.1 1
netsh interface ip set dns name="Loca Area Connection" static 192.168.1.1

The order of address in that command is IPAddress, Subnet Mask, Local Gateway. The address in the second command is the DNS server.

Comments

  • Anonymous
    September 23, 2008
    PingBack from http://www.easycoded.com/programmatically-changing-your-ip-address/
  • Anonymous
    September 23, 2008
    Would it not be more practical for them to use the alternate configuration option for the IPv4 binding?Use DHCP in the first instance, and then if it can't find a DHCP server, it'll drop down and use the values in the alternate configuration.
  • Anonymous
    September 23, 2008
    Yeah unfortunately that is giving her fits too. I set that up and it worked while I was there but the next day it didn't work. This is the third time that I've thought I fixed it but it didn't work. I don't know what's going on, but I'm pretty confident this will work and I can put it behind me.
  • Anonymous
    February 02, 2017
    Following is a complete app in c# for setting the IPs for Wifi and Ethernet.https://github.com/kamran7679/ConfigureIP.git