-3

I am trying to write an application to be able to change a few registry values.Such as Dns Server,Defalut Gateway .I am using this code below for doing this

 RegistryKey openSubKey = Registry.LocalMachine.OpenSubKey(path, true);
            if (openSubKey != null)
            {

                //HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\services\Tcpip\Parameters\Interfaces\{F7DFBC05-B946-4C27-A58B-13BFB3FCC04E}


                    openSubKey.SetValue("IPAddress", "192.168.2.132");
                    openSubKey.SetValue("SubnetMask", "255.255.255.0");
                    openSubKey.SetValue("DefaultGateway", "192.168.2.2");
                    openSubKey.SetValue("NameServer", ""192.168.2.132,192.168.2.132"");

.Actually code works .I can see the new values in th registry as you can see

enter image description here

However when I check network connections I have realized that nothing changed but NameServer.What am I doing wrong here.

enter image description here

digrev07
  • 53
  • 3
  • Is it possible to change your IP address when it's configured to pick up settings from DHCP? I seriously doubt it. I'm not sure that changing the registry is the correct way to set these values. – spender Mar 24 '15 at 16:20
  • 3
    You should use a documented API for this, such as WMI – Alex K. Mar 24 '15 at 16:20
  • You haven't uttered the Simsalabim incantation that forces the TCP stack to read the registry again. Or used the correct registry key. Yes, use WMI instead. – Hans Passant Mar 24 '15 at 16:26

1 Answers1

1

My suggestion is instead of direct registry manipulation, use WMI. See this StackOverflow post or this Code Project article on using WMI to update the Network Configuration.

Mostly you will have to work with Win32_NetworkAdapterConfiguration WMI object.

Community
  • 1
  • 1
Ganesh R.
  • 4,337
  • 3
  • 30
  • 46