I have an application written in C# that needs to be able to configure the network adapters in Windows. I have this basically working through WMI, but there are a couple of things I don't like about that solution: sometimes the settings don't seem to stick, and when the network cable is not plugged in, errors are returned from the WMI methods, so I can't tell if they really succeeded or not.
I need to be able to configure all of the settings available through the network connections - Properties - TCP/IP screens.
What's the best way to do this?
With my code SetIpAddress and SetDHCP
You could use
Process
to fire off netsh commands to set all the properties in the network dialogs.eg: To set a static ipaddress on an adapter
To set it to dhcp you'd use
To do it from C# would be
Setting to static can take a good couple of seconds to complete so if you need to, make sure you wait for the process to exit.
Checkout this app. it is a complete application to set both wifi and ethernet ips
https://github.com/kamran7679/ConfigureIP
I can tell you the way the trojans do it, after having to clean up after a few of them, is to set registry keys under HKEY_LOCAL_MACHINE. The main ones they set are the DNS ones and that approach definitely sticks which can be atested to by anyone who has ever been infected and can no longer get to windowsupdate.com, mcafee.com etc.
with the help of @PaulB's answers help