using System;
using System.Management;
publicclassChangeIP
{
privateManagementBaseObject iObj = null;
privateManagementBaseObject oObj = null;
privateManagementClass mc = newManagementClass("Win32_NetworkAdapterConfiguration");
privatereadonlyManagementObjectCollection moc;
///
/// example:
///
/// ChangeIP o = new ChangeIP();
/// string[] ipList = new string[]{"192.168.0.253","192.168.0.250"};
/// string[] subnetList = new string[]{"255.255.255.0","255.255.255.0"};
/// o.ChangeTo(ipList,subnetList);
///
///
public ChangeIP()
{
moc = mc.GetInstances();
}
///
cortrol///IPAddr List
///subnetMask List
publicvoid ChangeTo(string[] ipAddr, string[] subnetMask)
{
foreach (ManagementObject mo in moc)
{
if (!(bool)mo["IPEnabled"]) continue;
iObj = mo.GetMethodParameters("EnableStatic");
iObj["IPAddress"] = ipAddr;
iObj["SubnetMask"] = subnetMask;
oObj = mo.InvokeMethod("EnableStatic", iObj, null);
}
}
///
cortrol///IPAddr List
///subnetMask List
///gateway List
///gateway CostMetric List, example: 1
publicvoid ChangeTo(string[] ipAddr, string[] subnetMask, string[] gateways, string[] gatewayCostMetric)
{
foreach (ManagementObject mo in moc)
{
if (!(bool)mo["IPEnabled"]) continue;
iObj = mo.GetMethodParameters("EnableStatic");
iObj["IPAddress"] = ipAddr;
iObj["SubnetMask"] = subnetMask;
oObj = mo.InvokeMethod("EnableStatic", iObj, null);
iObj = mo.GetMethodParameters("SetGateways");
iObj["DefaultIPGateway"] = gateways;
iObj["GatewayCostMetric"] = gatewayCostMetric;
oObj = mo.InvokeMethod("SetGateways", iObj, null);
}
}
///
cortrol///IPAddr List
///subnetMask List
///gateway List
///gateway CostMetric List, example: 1
///DNSServer List
publicvoid ChangeTo(string[] ipAddr, string[] subnetMask, string[] gateways, string[] gatewayCostMetric, string[] dnsServer)
{
foreach (ManagementObject mo in moc)
{
if (!(bool)mo["IPEnabled"]) continue;
iObj = mo.GetMethodParameters("EnableStatic");
iObj["IPAddress"] = ipAddr;
iObj["SubnetMask"] = subnetMask;
oObj = mo.InvokeMethod("EnableStatic", iObj, null);
iObj = mo.GetMethodParameters("SetGateways");
iObj["DefaultIPGateway"] = gateways;
iObj["GatewayCostMetric"] = gatewayCostMetric;
oObj = mo.InvokeMethod("SetGateways", iObj, null);
iObj = mo.GetMethodParameters("SetDNSServerSearchOrder");
iObj["DNSServerSearchOrder"] = dnsServer;
oObj = mo.InvokeMethod("SetDNSServerSearchOrder", iObj, null);
}
}
///
DHCPEnabledpublicvoid EnableDHCP()
{
foreach (ManagementObject mo in moc)
{
if (!(bool)mo["IPEnabled"]) continue;
if (!(bool)mo["DHCPEnabled"])
{
iObj = mo.GetMethodParameters("EnableDHCP");
oObj = mo.InvokeMethod("EnableDHCP", iObj, null);
}
}
}
}
txtip.Text = "192.168.1.120";
string scmd = "/c netsh.exe interface ip set address 本地连接 static " + txtip.Text + " 255.255.255.0 192.168.1.1 1";
System.Diagnostics.Process.Start("cmd.exe",scmd);
这是网络编程吧
兔~子转换器多个地区
500多个城市
每天不足
多个地区任意使用
标签:C#,IP,更换