1

I want to configure all active network adapters in windows 7 programatically through c#.

I have tried following 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;
            }
        }

But it just updates the default gateways and changes nothing else.

I have used netsh command as well:

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();

        }

But it works for first adapter and after that it thows an error:

"Failed to configure the DHCP service. The interface may be disconnected."

How can i configure all adapters in c#?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Demon Hunter
  • 233
  • 1
  • 3
  • 15
  • Run `ipconfig /all` in command line and you will see that it does indeed show disconnected network adapters, so it's not really throwing an error. – JMK Mar 14 '13 at 12:00
  • 1
    Thanks for reply. The only change see, in a enabled adapter after running netsh code is "Obtain IP address Automatically" is changed to "Use the Following IP" with no IP address.Also, I guess creating a new process everytime will not be an efficient way to configure the network adapters. We may end having lots of zoombie processes running. – Demon Hunter Mar 14 '13 at 12:22

1 Answers1

0

I know this post is old but I believe you are having this issue because you are trying to set the IP of multiple adaptors to the Exact same IP.

  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – djv Nov 26 '14 at 15:25
  • I recently had the same issue as Demon Hunter and it was because i was inadvertently trying to set all adaptors to the exact same IP. I was just trying to help! – Ken Nichols Nov 29 '14 at 19:46