1

I want to set an static ip (IPv4) to my LAN adapter settings. Former state: LAN adapter "XY" settings set to auto ip configuration Wanted state: LAN adapter "XY" settings set to an static ip and subnet mask

With the following code I can change my ip and subnet mask only when static ip is activated, but I want to change it also when auto ip config is activated. I don't know how to modify 'if (!(bool)objMO["IPEnabled"])' so that I can choose the LAN adapter "XY". Currently the LAN adapter with static ip is chosen for ip change.

ManagementClass objMC = new ManagementClass(
            "Win32_NetworkAdapterConfiguration");
        ManagementObjectCollection objMOC = objMC.GetInstances();

        foreach (ManagementObject objMO in objMOC)
        {
            if (!(bool)objMO["IPEnabled"])
                    continue;

            try
            {
                ManagementBaseObject objNewIP = null;
                ManagementBaseObject objSetIP = null;

                objNewIP = objMO.GetMethodParameters("EnableStatic");

                //Set IPAddress and Subnet Mask
                objNewIP["IPAddress"] = new string[] { IPAddress };
                objNewIP["SubnetMask"] = new string[] { SubnetMask };

                objSetIP = objMO.InvokeMethod("EnableStatic", objNewIP, null);

            }

        }

Nearly I have the same question as (here), but I want to change the network configuration of a fixed LAN-adapter name.

Community
  • 1
  • 1
David
  • 65
  • 10
  • Sorry, what? When your adapter is configured to automatically select an IP address, you want to be able to change the IP address? If you could do that, wouldn't it defeat the whole point of the adapter being configured for automatic? And the code you have, what happens if you have more than one candidate adapter? It looks like you would wind up with them fighting over the same IP address. Is that really what you want? I'm finding this question very unclear. – Peter Duniho Aug 28 '16 at 01:13
  • @PeterDuniho I'm sorry for that. I have 4 adapters I can see in my settings. Bluetooth, LAN, WLAN and VMware. I want to switch only the LAN adapter from automatically select an IP to an fixed static IP address. If I set the LAN adapter manually to static ip and type in a random IP it works to change the IP of the adapter with my code. I think the problem is the 'if IP is enabled' part in my code. So that the adapter is only chosen, when it is already set to static. I hope it is a bit clearer now – David Aug 29 '16 at 05:26
  • Sorry, I still don't understand. The only reason the adapter is only chosen when it's already set to static is because you explicitly wrote the loop to skip the adapter if it wasn't. If that's not the logic you want, then don't write the loop that way. Instead, find the adapter you want and explicitly change its mode to static when you do. – Peter Duniho Aug 29 '16 at 05:32
  • Yes, this is what I want to do, but I don't know how I can search the name of the adapter. I need some code to read out the LAN adapters name. Do you know how a can do that? – David Aug 29 '16 at 05:36
  • You should be able to get the adapter name using the `objMO["Caption"]` property in the code you have above. Did you try that? – Peter Duniho Aug 29 '16 at 06:03
  • If I use 'objMO["Caption"] == "AdapterName"' its still not working (but it also works to change it when ip static is already active... I tried the real Name of the Adapter "Intel ...." and my preconfigured name "LAN". – David Aug 29 '16 at 17:20
  • Just started the program with admin privilegs. now the ip is set from auto to static but every adapter wlan, lan, ... is changed to static (with no numbers in it) – David Aug 29 '16 at 17:27
  • @PeterDuniho just found this one objMO["Caption"].Equals("AdapterName"). But this is also not working... – David Aug 29 '16 at 17:51
  • Finally got it: `using System.Net.NetworkInformation;` `NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adapter in interfaces) { initializeNames = adapter.Name; }` I think issue can be closen thank for you help @PeterDuniho . – David Aug 29 '16 at 22:20

0 Answers0