0

I've been trying to implement the solution from this post: How can you change Network settings (IP Address, DNS, WINS, Host Name) with code in C# but for some reason it doesn't seem to work in my situation and I can't pinpoint the problem. I have this method:

 public void SetDNS(string NIC, string DNS)
        {
            ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection objMOC = objMC.GetInstances();

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

                    if (objMO["Caption"].Equals(NIC))
                    {

                        ManagementBaseObject newDNS = objMO.GetMethodParameters("SetDNSServerSearchOrder");
                        newDNS["DNSServerSearchOrder"] = DNS.Split(',');
                        _ = objMO.InvokeMethod("SetDNSServerSearchOrder", newDNS, null);
                        System.Diagnostics.Debug.WriteLine("testing");

                    }
                }
            }
        }

And then I call it like this SetDNS(objMO["Caption"].toString(), "192.168.7.2,8.8.8.8"); where objMO["Caption"] is the NIC description of the current network adapter I want to configure. When I run the debug to test the program, nothing happens to the network adapter settings but in the debug output window "testing" does get printed out so I know that the method executed. Is there something obvious that I am missing here? I'm new to WPF and win32 so I apologize in advance if there is something I'm missing, thanks.

EDIT: When I checked the return value of SetDNSServerSearchOrder it returned an error code of 91 which means Access Denied. Does this mean I need elevation for the whole program and if so how do I achieve that?

Sparky
  • 68
  • 5
  • Add this: `var resultObj = objMO.InvokeMethod("SetDNSServerSearchOrder", newDNS, null);` (so you don't discard the result), then `uint result = (uint)resultObj.GetPropertyValue("ReturnValue");`. See what response you get back: `result = 0` if all went well, `1` if the setting change needs a reboot, otherwise it's an error code. Of course, also add `break;` to the loop, so you break out when you have found and changed a value in Network Interface specified – Jimi Apr 09 '20 at 18:48
  • Thanks, I should have thought of that to test for it. The value returned was 91 which when I looked it up it meant access denied. Is there a workaround that I could implement? – Sparky Apr 09 '20 at 20:08
  • First try to run your app elevated (throught `app.manifest` configuration - or whatever else you prefer), see whether you get the same error code. Otherwise, you simply cannot change the settings of that Interface. BTW, we usually use the `Description`, not the `Caption` property. `Caption` may contain indexing values that do not appear anywhere else, while `Description` is the same name you can see in the Network manager panel. Same as `NetworkInterface.GetAllNetworkInterfaces()`, then reading the `[NetworkInterface].Description` property. – Jimi Apr 09 '20 at 20:34

1 Answers1

0

Yep, running my app elevated through app.manifest configuration fixed it for me. All I needed was elevated permissions. Thanks for help!

Sparky
  • 68
  • 5