Set network configuration programmatically c#

2019-07-12 03:05发布

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

标签: c# .net wmi
1条回答
姐就是有狂的资本
2楼-- · 2019-07-12 03:28

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

查看更多
登录 后发表回答