2011-10-06 164 views
2

我要救我的網絡適配器配置,然後向另一臺計算機上恢復。我使用WMI讓我的網絡配置和我將它保存到.txt文件:設置網絡配置編程C#

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(); 
       } 
      } 
     } 

,然後,當我想在另一臺計算機上設置的TCP/IP設置,我讀的文件的信息:

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()); 
      } 

,並設置新的設置:

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); 
      } 
     } 
    } 

但問題是,當我檢查我的TCP/IP配置,它永遠不會改變..... 我不知道如何得到它工作....

+0

引述IT人羣:「你試過將其關閉並重新開啓?」無論如何,ipconfig /所有輸出前後是什麼?你是否有可能首先設置其中一個內部適配器(之後用相同的值設置實際的適配器將不起作用)?你真的有網絡連接嗎?需要更多的信息.​​.我的應用程序之前 – mtijn

+0

@mtijn IPCONFIG顯示我的網絡適配器的原始設置,以後我的應用程序更改網絡設置IPCONFIG仍顯示原來的設置。 我沒有嘗試關閉它,但我運行該應用程序後重新啓動計算機。 我不知道我是否先設置內部適配器...... 我有一個網絡連接.... –

+0

登錄你使用此功能設置適配器的原始IP和尋找奇數值,這些都是您的內部適配器。另外,你是否會遇到任何異常(包括你正在捕捉的異常)? – mtijn

回答

1

看看這個question - 不同的方法,但應該工作正常。

+0

謝謝你,它的工作!我的應用程序可以更改IP,子網和網關....(現在我必須更改DNS和WINS).....非常感謝! –