Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Network Conf in C#

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 ?

like image 773
Deathdigger Avatar asked Oct 08 '22 18:10

Deathdigger


1 Answers

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".

like image 127
Prafulla Avatar answered Oct 12 '22 12:10

Prafulla