title: Configure Win 7 Network Adapter Programatically in C#
body: <p>I want to configure all active network adapters in windows 7 programatically through c#.</p>
  
  <p>I have tried following code: </p>
  
  <pre><code>string newIPAddress = "100.200.100.11";
          string newSubnetMask = "255.255.255.1";
          string[] newGateway = { "100.200.100.1" };
  
          ManagementObjectSearcher m = new ManagementObjectSearcher();
          m.Query = new ObjectQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True");
          foreach (ManagementObject mo in m.Get())
          {
              try
              {
                  ManagementBaseObject setIP;
                  ManagementBaseObject newIP = mo.GetMethodParameters("EnableStatic");
  
                  newIP["IPAddress"] = new string[] { newIPAddress };
                  newIP["SubnetMask"] = new string[] { newSubnetMask };
  
                  setIP = mo.InvokeMethod("EnableStatic", newIP, null);
                  mo.InvokeMethod("SetGateways", new object[] { newGateway, new string[] { "1" } });
                  mo.InvokeMethod("SetDNSServerSearchOrder", new object[] { new string[] { "100.100.100.100" } });
              }
              catch (Exception)
              {
                  throw;
              }
          }
  </code></pre>
  
  <p>But it just updates the default gateways and changes nothing else.</p>
  
  <p>I have used netsh command as well:</p>
  
  <pre><code>NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
          foreach (NetworkInterface adapter in adapters)
          {
              Console.WriteLine(adapter.Name);
              Process p = new Process();
              ProcessStartInfo psi = new ProcessStartInfo("netsh", "interface ip set address \"" + adapter.Name + "\" static 192.168.0.10 255.255.255.0 192.168.0.1 ");
              psi.UseShellExecute = false;
              p.StartInfo = psi;
              p.Start();
  
          }
  </code></pre>
  
  <p>But it works for first adapter and after that it thows an error:</p>
  
  <p>"Failed to configure the DHCP service. The interface may be disconnected."</p>
  
  <p>How can i configure all adapters in c#?</p>