使用WMI控制网络适配器的类(获取所有启用的网络适配器及其信息。设置IP、子网掩码,网关,DNS)
来源:互联网 发布:读英语软件 编辑:程序博客网 时间:2024/05/20 12:25
使用WMI控制网络适配器的类(获取所有启用的网络适配器及其信息。设置IP、子网掩码,网关,DNS)
using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
namespace SetupNetwork
{
/// <summary>
/// 表示一个IP地址,包括IP和子网掩码
/// </summary>
class IP
{
string ip, submask;
public IP(string ip, string submask)
{
this.ip = ip;
this.submask = submask;
}
public string IPAddress
{
get
{
return ip;
}
set { ip = value; }
}
public string Submask
{
get { return submask; }
set { submask = value; }
}
}
/// <summary>
/// 表示一个网关,包括风网关和跳
/// </summary>
class Gateway
{
string ip;
UInt16 heap;
//private new Gateway() { }
public Gateway(string ip, UInt16 heap)
{
this.ip = ip;
this.heap = heap;
}
public string IP
{
get { return ip; }
set { ip = value; }
}
public UInt16 Heap
{
get { return heap; }
set { heap = value; }
}
}
class NetworkInfomation
{
string m_Name;//Network Adpter's name
IP[] ip;
Gateway[] gateway;
string[] m_dns;
string m_MAC;
ManagementObject mo;
/// <summary>
///
/// </summary>
/// <param name="AdpterIndex">网络适配器的序号零基</param>
public NetworkInfomation(int AdpterIndex)
{
ManagementObjectSearcher mc = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE'");
ManagementObjectCollection moc = mc.Get();
int idx = 0;
foreach (ManagementObject mo in moc)
{
if (idx++ == AdpterIndex)
{
if ((bool)mo["IPEnabled"])
{
this.mo = mo;
m_Name = (string)mo["Description"];
m_MAC = (string)mo["MacAddress"];
// 取得本机IP地址,并显示
string[] addresses = (string[])mo["IPAddress"];
// 取得本机子网掩码,并显示
string[] subnets = (string[])mo["IPSubnet"];
ip = new IP[addresses.Length];
for (int i = 0; i < addresses.Length; i++)
{
ip[i] = new IP(addresses[i], subnets[i]);
//ip[i].IPAddress = ;
//ip[i].Submask = s;
}
// 检测IP冲突
if (ip.Length == 1 && ip[0].IPAddress == "0.0.0.0")
{
throw new Exception("IP冲突");
}
// 取得网关,并显示
string[] gateways = (string[])mo["DefaultIPGateway"];
System.UInt16[] costMetric = (System.UInt16[])mo["GatewayCostMetric"];
gateway = new Gateway[gateways.Length];
for (int i = 0; i < gateways.Length; i++)
{
gateway[i] = new Gateway(gateways[i], costMetric[i]);
//gateway[i].IP = ;
//gateway[i].Heap = ;
}
// 取得DNS服务器地址,并显示
m_dns = (string[])mo["DNSServerSearchOrder"];
break;
}
}
}
}
/// <summary>
/// 获取网络适配器的MAC
/// </summary>
public string MAC
{
get { return m_MAC; }
}
/// <summary>
/// 获取所有启用的网络适配器的名称
/// </summary>
/// <returns></returns>
public static string[] GetNetworkAdpters()
{
ManagementObjectSearcher mc = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE'");
ManagementObjectCollection moc;
moc = mc.Get();
string[] s = new string[moc.Count];
int i = 0;
foreach (ManagementObject mo in moc)
{
s[i++] = mo["Description"].ToString();
}
return s;
}
/// <summary>
/// 网络适配器名称
/// </summary>
public string Name
{
get { return m_Name; }
}
/// <summary>
/// 分配在网络适配器上的IP地址
/// </summary>
public IP[] IP
{
get { return ip; }
}
/// <summary>
/// 分配在网络适配器上的网关
/// </summary>
public Gateway[] Gateway
{
get { return gateway; }
}
/// <summary>
/// 网络适配器上设置的的DNS服务器
/// </summary>
public string[] DNS
{
get { return m_dns; }
}
/// <summary>cortrol</summary>
/// <param name="ipAddr">IPAddr List</param>
/// <param name="subnetMask">subnetMask List</param>
public void ChangeTo(string[] ipAddr, string[] subnetMask)
{
ManagementBaseObject iObj = null;
ManagementBaseObject oObj = null;
iObj = mo.GetMethodParameters("EnableStatic");
iObj["IPAddress"] = ipAddr;
iObj["SubnetMask"] = subnetMask;
oObj = mo.InvokeMethod("EnableStatic", iObj, null);
}
/// <summary>设置网络适配器IP</summary>
/// <param name="ipAddr">IPAddr List</param>
/// <param name="subnetMask">subnetMask List</param>
public void ChangeTo(IP[] ip)
{
string[] sip = new string[ip.Length];
string[] sub = new string[ip.Length];
for (int i = 0; i < ip.Length; i++)
{
sip[i] = ip[i].IPAddress;
sub[i] = ip[i].Submask;
}
ChangeTo(sip, sub);
}
/// <summary>设置网络适配器IP</summary>
/// <param name="ipAddr">IPAddr List</param>
/// <param name="subnetMask">subnetMask List</param>
public static void ChangeTo(string[] ipAddr, string[] subnetMask, int index)
{
ManagementBaseObject iObj = null;
ManagementBaseObject oObj = null;
ManagementObjectSearcher mc = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE'");
ManagementObjectCollection moc;
moc = mc.Get();
int i = 0;
foreach (ManagementObject mo in moc)
{
if (!(bool)mo["IPEnabled"]) continue;
if (i++ == index)
{
iObj = mo.GetMethodParameters("EnableStatic");
iObj["IPAddress"] = ipAddr;
iObj["SubnetMask"] = subnetMask;
oObj = mo.InvokeMethod("EnableStatic", iObj, null);
break;
}
}
}
/// <summary>设置网络适配器IP</summary>
/// <param name="ipAddr">IPAddr List</param>
/// <param name="subnetMask">subnetMask List</param>
public static void ChangeTo(IP[] ip, int index)
{
string[] sip = new string[ip.Length];
string[] sub = new string[ip.Length];
for (int i = 0; i < ip.Length; i++)
{
sip[i] = ip[i].IPAddress;
sub[i] = ip[i].Submask;
}
NetworkInfomation.ChangeTo(sip, sub, index);
}
}
}
- 使用WMI控制网络适配器的类(获取所有启用的网络适配器及其信息。设置IP、子网掩码,网关,DNS)
- 获得本地所有的网络适配器及其信息
- VB6获取本机网络适配器信息(网卡名称、IP地址、子网掩码、默认网关等)
- 获取当前可使用的网卡并检查网卡设置(网卡的IP,网关,子网掩码,DNS,备用DNS信息)
- 获取wifi的ip,子网掩码,网关,dns之类的信息
- android获取wifi的IP,子网掩码,网关,dns等信息
- android获取wifi的IP,子网掩码,网关,dns等信息
- android获取wifi的IP,子网掩码,网关,dns等信息
- 获取并修改本机的ip,网关,dns,子网掩码等信息
- 获得及设置本机的ip地址,子网掩码,网关,dns服务器信息
- c#获取本地连接的ip 子网掩码 网关 DNS
- 获取网络适配器的相关信息(包括网络连接名称)
- IP,子网掩码,网关,DNS的关系解析
- IP、子网掩码、网关、dns的区别
- 关于 子网掩码,IP,网关,DNS 的概述
- 更改Win 2003网络适配器的IP地址
- 在vb中使用Iphlpapi.dll获取网络信息 第二章 第一节 获取本机网络适配器的信息
- 在vb中使用Iphlpapi.dll获取网络信息 第二章 第一节 获取本机网络适配器的信息
- windows2003 server 中文 sp2 下载地址:
- 找到内存泄漏
- Directshow捕捉摄像头视频的类CameraCapture(SDK)
- 主窗口的诞生过程
- CSDN的Blog文章中插入图片
- 使用WMI控制网络适配器的类(获取所有启用的网络适配器及其信息。设置IP、子网掩码,网关,DNS)
- 控制显示器的类
- 天大四才子--孟宪堂之经典语录
- dct
- c++学习杂记(1)
- Collection Map 深入剖析
- translate函数的使用
- 刚来的开始
- 浅谈WEBGIS运用栅格地图实现原理[更新:Google Maps带来的新型WebGIS设计模式]