C#设置IP地址,启用禁用适配器
作者:网络转载 发布时间:[ 2015/6/17 15:00:10 ] 推荐标签:软件开发
2 设置IP为自动获取
/// <summary>
/// 启用DHCP服务
/// </summary>
public void EnableDHCP()
{
ManagementClass wmi = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = wmi.GetInstances();
foreach (ManagementObject mo in moc)
{
if (!(bool)mo["IPEnabled"])
continue;
if (mo["SettingID"].ToString() == this.NetworkInterfaceID) //网卡接口标识是否相等, 相当只设置指定适配器IP地址
{
mo.InvokeMethod("SetDNSServerSearchOrder", null);
mo.InvokeMethod("EnableDHCP", null);
}
}
}
3. 获取所有适配器
public class NetworkAdapterUtil
{
/// <summary>
/// 获取所有适配器类型,适配器被禁用则不能获取到
/// </summary>
/// <returns></returns>
public List<NetworkAdapter> GetAllNetworkAdapters() //如果适配器被禁用则不能获取到
{
IEnumerable<NetworkInterface> adapters = NetworkInterface.GetAllNetworkInterfaces(); //得到所有适配器
return GetNetworkAdapters(adapters);
}
/// <summary>
/// 根据条件获取IP地址集合,
/// </summary>
/// <param name="adapters">网络接口地址集合</param>
/// <param name="adapterTypes">网络连接状态,如,UP,DOWN等</param>
/// <returns></returns>
private List<NetworkAdapter> GetNetworkAdapters(IEnumerable<NetworkInterface> adapters, params NetworkInterfaceType[] networkInterfaceTypes)
{
adapterList = new List<NetworkAdapter>();
foreach (NetworkInterface adapter in adapters)
{
if (networkInterfaceTypes.Length <=0) //如果没传可选参数,查询所有
{
if (adapter != null)
{
NetworkAdapter adp = SetNetworkAdapterValue(adapter);
adapterList.Add(adp);
}
else
{
return null;
}
}
else //过滤查询数据
{
foreach (NetworkInterfaceType networkInterfaceType in networkInterfaceTypes)
{
if (adapter.NetworkInterfaceType.ToString().Equals(networkInterfaceType.ToString()))
{
adapterList.Add(SetNetworkAdapterValue(adapter));
break; //退出当前循环
}
}
}
}
return adapterList;
}
/// <summary>
/// 设置网络适配器信息
/// </summary>
/// <param name="adapter"></param>
/// <returns></returns>
private NetworkAdapter SetNetworkAdapterValue(NetworkInterface adapter)
{
NetworkAdapter networkAdapter = new NetworkAdapter();
IPInterfaceProperties ips = adapter.GetIPProperties();
networkAdapter.Description = adapter.Name;
networkAdapter.NetworkInterfaceType = adapter.NetworkInterfaceType.ToString();
networkAdapter.Speed = adapter.Speed / 1000 / 1000 + "MB"; //速度
networkAdapter.MacAddress = adapter.GetPhysicalAddress(); //物理地址集合
networkAdapter.NetworkInterfaceID = adapter.Id;//网络适配器标识符
networkAdapter.Getwaryes = ips.GatewayAddresses; //网关地址集合
networkAdapter.IPAddresses = ips.UnicastAddresses; //IP地址集合
networkAdapter.DhcpServerAddresses = ips.DhcpServerAddresses;//DHCP地址集合
networkAdapter.IsDhcpEnabled =ips.GetIPv4Properties()==null?false: ips.GetIPv4Properties().IsDhcpEnabled; //是否启用DHCP服务
IPInterfaceProperties adapterProperties = adapter.GetIPProperties();//获取IPInterfaceProperties实例
networkAdapter.DnsAddresses = adapterProperties.DnsAddresses; //获取并显示DNS服务器IP地址信息 集合
return networkAdapter;
}
}
}
4 启用, 禁用适配器代码
/// <summary>
/// 启用所有适配器
/// </summary>
/// <returns></returns>
public void EnableAllAdapters()
{
// ManagementClass wmi = new ManagementClass("Win32_NetworkAdapter");
// ManagementObjectCollection moc = wmi.GetInstances();
System.Management.ManagementObjectSearcher moc = new System.Management.ManagementObjectSearcher("Select * from Win32_NetworkAdapter where NetEnabled!=null ");
foreach (System.Management.ManagementObject mo in moc.Get())
{
//if (!(bool)mo["NetEnabled"])
// continue;
string capation = mo["Caption"].ToString();
string descrption =mo["Description"].ToString();
mo.InvokeMethod("Enable",null);
}
}
/// <summary>
/// 禁用所有适配器
/// </summary>
public void DisableAllAdapters()
{
// ManagementClass wmi = new ManagementClass("Win32_NetworkAdapter");
// ManagementObjectCollection moc = wmi.GetInstances();
System.Management.ManagementObjectSearcher moc = new System.Management.ManagementObjectSearcher("Select * from Win32_NetworkAdapter where NetEnabled!=null ");
foreach (System.Management.ManagementObject mo in moc.Get())
{
//if ((bool)mo["NetEnabled"])
// continue;
string capation = mo["Caption"].ToString();
string descrption = mo["Description"].ToString();
mo.InvokeMethod("Disable", null);
}
}
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系SPASVO小编(021-61079698-8054),我们将立即处理,马上删除。
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11热门文章
常见的移动App Bug??崩溃的测试用例设计如何用Jmeter做压力测试QC使用说明APP压力测试入门教程移动app测试中的主要问题jenkins+testng+ant+webdriver持续集成测试使用JMeter进行HTTP负载测试Selenium 2.0 WebDriver 使用指南