2

I want to save my network adapter configuration and then to restore it on another computer. I use WMI to get my network configurations and i save it to the .txt file :

   using (TextWriter tw = new StreamWriter(@"D:\\NetworkConfiguration.txt"))
        {
            if (tw != null)
            {
                ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");

                ManagementObjectCollection objMOC = objMC.GetInstances();

                foreach (ManagementObject objMO in objMOC)
                {
                    if (!(bool)objMO["ipEnabled"])
                        continue;

                    string[] ipaddresses = (string[])objMO["IPAddress"];
                    string[] subnets = (string[])objMO["IPSubnet"];
                    string[] gateways = (string[])objMO["DefaultIPGateway"];



                    tw.WriteLine("IpAdresses");
                    foreach (string sIP in ipaddresses)
                        tw.WriteLine(sIP);

                    tw.WriteLine("IpSubnets");
                    foreach (string sNet in subnets)
                        tw.WriteLine(sNet);

                    tw.WriteLine("Gateways");
                    foreach (string sGate in gateways)
                        tw.WriteLine(sGate);


                    // close the stream
                    tw.Close();
                }
            }
        }

and then , when i want to set tcp/ip settings on another computer , i read the file's information :

 using (TextReader tr = new StreamReader(@"D:\\NetworkConfiguration.txt"))
            {
                List<string> ipAddrr = new List<string>();
                List<string> ipSubnet = new List<string>();
                List<string> Gateway = new List<string>();

                string line = tr.ReadLine();
                while (line != null)
                {
                    if (line.Equals("IpAdresses"))
                    {
                        ipAddrr.Add(tr.ReadLine());
                        ipAddrr.Add(tr.ReadLine());
                    }
                    if (line.Equals("IpSubnets"))
                    {
                        ipSubnet.Add(tr.ReadLine());
                        ipSubnet.Add(tr.ReadLine());
                    }
                    if (line.Equals("Gateways"))
                    {
                        Gateway.Add(tr.ReadLine());
                    }
                    line = tr.ReadLine();
                }

                setIP(ipAddrr.ToArray(), ipSubnet.ToArray(), Gateway.ToArray());
            }

and set the new setting :

public void setIP(string[] IPAddress, string[] SubnetMask, string[] Gateway)
    {

        ManagementClass objMC = new ManagementClass(
            "Win32_NetworkAdapterConfiguration");
        ManagementObjectCollection objMOC = objMC.GetInstances();


        foreach (ManagementObject objMO in objMOC)
        {

            if (!(bool)objMO["IPEnabled"])
                continue;



            try
            {
                ManagementBaseObject objNewIP = null;
                ManagementBaseObject objSetIP = null;
                ManagementBaseObject objNewGate = null;


                objNewIP = objMO.GetMethodParameters("EnableStatic");
                objNewGate = objMO.GetMethodParameters("SetGateways");



                //Set DefaultGateway

                objNewGate["DefaultIPGateway"] = Gateway ;
                objNewGate["GatewayCostMetric"] = new int[] { 1 };


                //Set IPAddress and Subnet Mask

                objNewIP["IPAddress"] = IPAddress;
                objNewIP["SubnetMask"] = SubnetMask;

                objSetIP = objMO.InvokeMethod("EnableStatic", objNewIP, null);
                objSetIP = objMO.InvokeMethod("SetGateways", objNewGate, null);



                MessageBox.Show(
                   "Updated IPAddress, SubnetMask and Default Gateway!");



            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to Set IP : " + ex.Message);
            }
        }
    }

but the problem is that when i check my tcp/ip configuration , it never changes..... i dont understand how to get it work....

N.D
  • 1,009
  • 5
  • 23
  • 39
  • to quote IT crowd: "have you tried turning it off and on again?" anyway, what does ipconfig /all output before and after? are you, by any chance, setting one of the internal adapters first (after which setting the actual adapter with the same values will not work)? do you actually have a network connection? need more info.. – mtijn Oct 06 '11 at 07:00
  • @mtijn before my application ipconfig shows me the original settings of the network adapter , and after my application changed the network settings ipconfig still shows the original setting . I didnt try to turn it off and on but i restarted the computer after running the application. I dont know if i am setting internal adapter first... I have a network connection.... – N.D Oct 06 '11 at 07:32
  • log the original ip of the adapters you're setting with this function and look for odd values, those are your internal adapters. also, are you getting any exceptions (including the one you're catching)? – mtijn Oct 06 '11 at 07:39
  • @mtijn No , i don't get any exceptions , the code works and after that when i check the network settings , i see they didn't change... – N.D Oct 06 '11 at 07:43
  • i have found this link very useful - [netsh commands](http://www.petri.co.il/configure_tcp_ip_from_cmd.htm) – N.D Oct 06 '11 at 13:21

1 Answers1

1

Take a look at this question - different method but should work ok.

Community
  • 1
  • 1
PaulB
  • 23,264
  • 14
  • 56
  • 75
  • thank you , it worked!!! my app can change ip,subnet and gateway.... (now i have to change DNS and WINS).....thanks a lot!!! – N.D Oct 06 '11 at 12:30