使用C#更改网络适配器的IP地址 [英] Change IP address of Network Adapter using C#
本文介绍了使用C#更改网络适配器的IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道如何使用此代码更改系统的IP地址。但这只会改变当前的连接。
I know how to change the IP address of the system using this code. But this will change only the current connection.
public void setIP(string ip_address, string subnet_mask)
{
ListAllNetworkAdapters();
ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection objMOC = objMC.GetInstances();
foreach (ManagementObject objMO in objMOC)
{
if ((bool)objMO["IPEnabled"])
{
try
{
ManagementBaseObject setIP;
ManagementBaseObject newIP =
objMO.GetMethodParameters("EnableStatic");
newIP["IPAddress"] = new string[] { ip_address };
newIP["SubnetMask"] = new string[] { subnet_mask };
setIP = objMO.InvokeMethod("EnableStatic", newIP, null);
}
catch (Exception)
{
throw;
}
}
}
}
我使用代码列出了多个网络适配器
I Listed the multiple network adaptors using the code
foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
{
if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
{
cmbAdaptors.Items.Add(ni.Name);
}
}
现在我想将每个适配器的IP地址更改为不同的IP地址。我想为WiFi添加IP地址,并为LAN电缆连接添加不同的IP。
如何使用代码更改所选网络适配器的IPv4地址。
Now I want to change the IP address of each adaptor to a different IP address. I want to put IP address for WiFi and put a different IP to LAN cable connection.
How I can change the IPv4 address of selected network adaptor using code.
推荐答案
我认为这篇文章可以帮助你,通过对不同的网络适配器进行分类,甚至分析它们以供下次使用!
SwitchNetConfig - 笔记本电脑用户,可以在不同的地方快速切换网络和代理配置 [ ^ ]
I think this article might help you, by classifying different network adapter and even profiling them for next use!
SwitchNetConfig - Laptop users, quickly switch network and proxy configuration in different places[^]
这篇关于使用C#更改网络适配器的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文