编程设置的网络配置的C# [英] Set network configuration programmatically c#
问题描述
我要救我的网络适配器配置,然后在另一台计算机还原它。我使用WMI让我的网络配置和我将它保存到.txt文件:
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();
}
}
}
然后,当我想在其他计算机上设置TCP / IP设置,我读文件的信息:
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());
}
和设置新的设置:
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配置,它永远不会改变..... 我不知道如何得到它的工作......
but the problem is that when i check my tcp/ip configuration , it never changes..... i dont understand how to get it work....
推荐答案
看看这个<一href="http://stackoverflow.com/questions/689230/best-way-to-programmatically-configure-network-adapters-in-net/760611#760611">question - 不同的方法,但应该确定
Take a look at this question - different method but should work ok.
这篇关于编程设置的网络配置的C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!