title: setup other computer network and configure the ip address programatically C#
body: <p>I have some sample codes where I can change my own ip address.</p>
  
  <p>Now i have a problem regarding changing the ip address of my networks. 
  When i mean networks (Computers that are connected to the lan)
  I want to connect and configure their ip address as well.</p>
  
  <p><strong>Example</strong></p>
  
  <p>I have 2 computers and the ip addresses are <code>192.168.1.6 - Computer 1</code> and <code>192.168.1.7 - Computer 2</code>. So now I am at computer 1 and I want to connect to <code>192.168.1.7</code> and change the ip address to <code>192.168.1.8</code></p>
  
  <p><strong>CODE</strong> </p>
  
  <pre><code>private void button1_Click_1(object sender, EventArgs e)
  {
  //example of ip 10.11.3.120
      try
      {
          setIP();
          setGateway();
      }
      catch(Exception ex)
      {
          MessageBox.Show(ex.ToString());
      }
      finally
      {
          setIP();
          setGateway();
          MessageBox.Show("Update Success");
  
  
      }
  }
  
  private void setIP()
  {
      ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
      ManagementObjectCollection objMOC = objMC.GetInstances();
  
      foreach (ManagementObject objMO in objMOC)
      {
          if ((bool)objMO["IPEnabled"])
          {
              try
              {
                  ManagementBaseObject setIP;
                  ManagementBaseObject newIP =
                      objMO.GetMethodParameters("EnableStatic");
                  //string ip_address = "10.11.3.120";
                  //string subnet_mask = "255.255.255.0";
                  newIP["IPAddress"] = new string[] { textBox1.Text };
  
                  newIP["SubnetMask"] = new string[] { textBox2.Text };
  
                  setIP = objMO.InvokeMethod("EnableStatic", newIP, null);
  
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.ToString());
              }
  
  
          }
      }
  }
  
  public void setGateway()
  {
      ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
      ManagementObjectCollection objMOC = objMC.GetInstances();
  
      foreach (ManagementObject objMO in objMOC)
      {
          if ((bool)objMO["IPEnabled"])
          {
              try
              {
                  ManagementBaseObject setGateway;
                  ManagementBaseObject newGateway =
                      objMO.GetMethodParameters("SetGateways");
  
                  newGateway["DefaultIPGateway"] = new string[] { textBox3.Text };
                  newGateway["GatewayCostMetric"] = new int[] { 1 };
  
                  setGateway = objMO.InvokeMethod("SetGateways", newGateway, null);
              }
              catch (Exception)
              {
                  throw;
              }
          }
      }
  }
  </code></pre>