开发者

Set network configuration programmatically c#

开发者 https://www.devze.com 2023-04-10 20:52 出处:网络
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 :

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["DefaultI开发者_高级运维PGateway"] = 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....


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

0

精彩评论

暂无评论...
验证码 换一张
取 消