5

I'm trying to make a simple program to change IP parameters in C#. I use this code : How can you change Network settings (IP Address, DNS, WINS, Host Name) with code in C# to perform it, and made my own method :

public void changerip(string adresse, string NIC)
{
    ManagementObjectCollection objMOC = objMC.GetInstances();
    foreach (ManagementObject objMO in objMOC)
    {

        if (objMO["Caption"].Equals(NIC))
        {
            try
            {
                if (adresse != "0")
                {
                    ManagementBaseObject setIP;
                    ManagementBaseObject newIP =
                        objMC.GetMethodParameters("EnableStatic");

                    newIP["IPAddress"] = new string[] { "192.168." + adresse + ".50" };
                    newIP["SubnetMask"] = new string[] { "255.255.255.0" };
                    setIP = objMO.InvokeMethod("EnableStatic", newIP, null);

                    ManagementBaseObject setGateway;
                    ManagementBaseObject newGateway =
                    objMO.GetMethodParameters("SetGateways");
                    newGateway["DefaultIPGateway"] = new string[] { "192.168." + adresse + ".254" };
                    newGateway["GatewayCostMetric"] = new int[] { 1 };
                    setGateway = objMO.InvokeMethod("SetGateways", newGateway, null);

                    ManagementBaseObject newDNS =
                    objMO.GetMethodParameters("SetDNSServerSearchOrder");
                    newDNS["DNSServerSearchOrder"] = "192.168.1.24,192.168.1.2".Split(',');
                    ManagementBaseObject setDNS =
                    objMO.InvokeMethod("SetDNSServerSearchOrder", newDNS, null);
                }
                else
                {
                    ManagementBaseObject newDNS = objMO.GetMethodParameters("SetDNSServerSearchOrder");
                    newDNS["DNSServerSearchOrder"] = null;
                    ManagementBaseObject enableDHCP = objMO.InvokeMethod("EnableDHCP", null, null);
                    ManagementBaseObject setDNS = objMO.InvokeMethod("SetDNSServerSearchOrder", newDNS, null);
                    //Save all Gateways into an array
                    string[] gateways = (string[])objMO["DefaultIPGateway"];

                    ManagementBaseObject newIP = objMO.GetMethodParameters("EnableStatic");
                    ManagementBaseObject newGate = objMO.GetMethodParameters("SetGateways");

                    //Set last value of the array(always the Gateway recived by DHCP) as the default Gateway
                    newGate["DefaultIPGateway"] = new string[] { gateways[gateways.Length - 1] };
                    newGate["GatewayCostMetric"] = new int[] { 1 };
                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }
    }
}

It works fine in Windows XP, but don't works on Windows 7 (Starter or Pro). I haven't any exception rising nor error/security message from the OS.

Someone have an idea of what happened ?

Community
  • 1
  • 1
Deathdigger
  • 53
  • 1
  • 3

1 Answers1

4

Looks like a credentials issue to me. You should run the program with Administrative privileges. You can do it by a simple Right click on exe and click "Run as Administrator".

Prafulla
  • 1,929
  • 1
  • 17
  • 21
  • Try this, and it doesn't works. Also, i tried to launch it logged with my username on the domain (thinking that it can be a certificate problem or something like that) with no result. – Deathdigger Feb 09 '12 at 11:29
  • In fact you were right ! Being administrator of the computer isn't the same thing as "running as administrator"... Also, you can't do it with the shortcut made with Visual Studio 2K10 Express, you have to create a new one manualy. Thanks for your help – Deathdigger Feb 13 '12 at 09:00
  • Also see http://stackoverflow.com/questions/2818179/how-do-i-force-my-net-application-to-run-as-administrator – Rocky Oct 07 '16 at 16:56