Click here to Skip to main content
12,624,887 members (35,851 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# Network
Hi,
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?
thanks.

code:
private void Form1_Load(object sender, EventArgs e)
{
    textBox1.Text = getip();
    setIP("192.168.97.51","255.255.255.0");
    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 =
                objMO.GetMethodParameters("EnableStatic");
 
            newIP["IPAddress"] = new string[] { ip_address };
            newIP["SubnetMask"] = new string[] { subnet_mask };
 
            setIP = objMO.InvokeMethod("EnableStatic", newIP, null);
        }
    }
}
Posted 20-Sep-12 0:01am
mehdi_k930
Comments
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
 
good
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);
  Permalink  
v4
Comments
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 | Mobile
Web02 | 2.8.161128.1 | Last Updated 20 Sep 2012
Copyright © CodeProject, 1999-2016
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