当前位置:百问十四>百科知识>C#更换IP功能如何实现。

C#更换IP功能如何实现。

2024-07-15 08:20:29 编辑:join 浏览量:541

C#更换IP功能如何实现。

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,更换

版权声明:文章由 百问十四 整理收集,来源于互联网或者用户投稿,如有侵权,请联系我们,我们会立即处理。如转载请保留本文链接:https://www.baiwen14.com/article/175533.html
热门文章